Telerik is a leading vendor of ASP.NET AJAX, ASP.NET MVC, Silverlight, WinForms and WPF controls and components, as well as .NET Reporting, .NET ORM , .NET CMS, Code Analysis, Mocking, Team Productivity and Automated Testing Tools. Building on its expertise in interface development and Microsoft technologies, Telerik helps customers build applications with unparalleled richness, responsiveness and interactivity. Telerik products help thousands of companies to be more productive and deliver reliable applications under budget and on time.
Version Q2 2011 released 07/12/2011
select

ToolBar / Add/Remove/Disable Items

Example Configuration
Add new button
Target: Checked state:
Add other items
Remove

Enable/Disable



Event log:

  • With RadToolBar it is easy to add, remove or disable items at the client side. What is more, you can persist the changes after postback.

    This example shows how to add/remove/disable RadToolBarItems, or add/remove buttons to/from the Buttons Collections of the RadToolBarDropDown and RadToolBarSplitButton.

    To add an item (a button, dropdown or a split button ) to the Items Collection of the toolbar:

    var toolBar = $find("<%= RadToolBar1.ClientID %>");
    
    //Creates a new item to be added to the toolbar's Items Collection. The line below creates a dropdown var newDropDown = new Telerik.Web.UI.RadToolBarDropDown(); newDropDown.set_text("Added client-side"); //Adds the dropdown to the Items Collection of the toolbar toolBar.get_items().add(newDropDown);

    To remove an item from the toolbar's Items Collection

    var toolBar = $find("<%= RadToolBar1.ClientID %>");
    if (toolBar.get_items().get_count() > 0)
        //Removes the last item from the toolbar's Items Collection
        toolBar.get_items().removeAt(toolBar.get_items().get_count() - 1);
    
    

    To disable a toolbar item

    var toolBar = $find("<%= RadToolBar1.ClientID %>");
    //Gets the last item in the toolbar's Items Collection
    var lastItem = toolBar.get_items().getItem(toolBar.get_items().get_count()-1);
    //Disables the last item
    lastItem.set_enabled(false);
    

    A similar approach is used to add/remove/disable buttons in the Buttons Collections of the RadToolBarDropDown or RadToolBarSplitButton.
    You can review the code sections of this example for more details.

    If you want to persist these changes after postback, the methods described below should be used:

    • toolBar.trackChanges();  - indicates that client-side changes are going to be made and these changes are supposed to be persisted after postback.
    • toolBar.commitChanges(); - submits the changes to the server.
    var toolBar = $find("RadToolBar1");
    toolBar.trackChanges();
    //add, remove, disable items
    toolBar.commitChanges();
    
    Client side changes are available on the server side after postback. You can use the ClientChanges property to access them:
    foreach (ClientOperation<RadToolBarItem> operation in RadToolBar1.ClientChanges)
    {
    	RadToolBarItem item = operation.Item;
    
    	switch (operation.Type)
    	{
    		case ClientOperationType.Insert:
    			break;
    		case ClientOperationType.Remove:
    			break;
    		case ClientOperationType.Update:
    			UpdateClientOperation<RadToolBarItem> update = operation as UpdateClientOperation<RadToolBarItem>;
    			break;
    		case ClientOperationType.Clear:
    			break;
    	}	
    }
    

Source Code

C# VB.NET
Show code in new window Demo isolation steps
  • <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DefaultCS.aspx.cs" Inherits="Telerik.Web.Examples.ToolBar.ClientSide.AddRemoveDisableItems.DefaultCS" %>

    <%@ Register TagPrefix="qsf" Namespace="Telerik.QuickStart" %>
    <%@ Register TagPrefix="qsf" TagName="Header" Src="~/Common/Header.ascx" %>
    <%@ Register TagPrefix="qsf" TagName="HeadTag" Src="~/Common/HeadTag.ascx" %>
    <%@ Register TagPrefix="qsf" TagName="Footer" Src="~/Common/Footer.ascx" %>
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <qsf:HeadTag runat="server" ID="Headtag1" />
    </head>
    <body class="BODY">
        <form runat="server" id="mainForm" method="post">
        <telerik:RadScriptManager ID="ScriptManager" runat="server" />

        <script type="text/javascript">
            //<![CDATA[

            function addButton() {
                var checked = $get("<%= cbChecked.ClientID %>").checked;

                var parent = determineParent();
                if (parent) {
                    addChildButton(parent, checked);
                }
                else {
                    alert("RadToolBar does not contain a parent item of the type selected!");
                }
                return false;
            }

            function determineParent() {
                var parentSelectionDropDown = $get("<%= ddlItem.ClientID %>");
                var selectedOption = parentSelectionDropDown.options[parentSelectionDropDown.selectedIndex].text;
                switch (selectedOption) {
                    case "ToolBar":
                        return $find("<%= RadToolBar1.ClientID %>");
                        break;
                    case "Last DropDown":
                        return findLastItemOfType(Telerik.Web.UI.RadToolBarDropDown);
                        break;
                    case "Last SplitButton":
                        return findLastItemOfType(Telerik.Web.UI.RadToolBarSplitButton);
                        break;
                }
            }

            function findLastItemOfType(itemType) {
                var toolBar = $find("<%= RadToolBar1.ClientID %>");
                var toolBarItems = toolBar.get_items();
                for (var i = toolBarItems.get_count() - 1; i >= 0; i--) {
                    var toolBarItem = toolBarItems.getItem(i);
                    if (itemType.isInstanceOfType(toolBarItem)) {
                        return toolBarItem;
                    }
                }
                return null;
            }

            function addChildButton(parent, checked) {
                var toolBar = $find("<%= RadToolBar1.ClientID %>");

                toolBar.trackChanges();

                var childrenCollection;
                if (parent.get_items) {
                    childrenCollection = parent.get_items();
                }
                else {
                    childrenCollection = parent.get_buttons();
                }

                var newButton = new Telerik.Web.UI.RadToolBarButton();
                newButton.set_text("Added client-side");
                if (checked) {
                    newButton.set_checked(checked);
                    newButton.set_checkOnClick(checked);
                }

                childrenCollection.add(newButton);

                toolBar.commitChanges();

                return false;
            }


            function addDropDown() {
                var toolBar = $find("<%= RadToolBar1.ClientID %>");
                var newDropDown = new Telerik.Web.UI.RadToolBarDropDown();
                newDropDown.set_text("Added client-side");
                toolBar.trackChanges();
                toolBar.get_items().add(newDropDown);
                toolBar.commitChanges();
                return false;
            }

            function addSplitButton() {
                var toolBar = $find("<%= RadToolBar1.ClientID %>");
                var newSplitButton = new Telerik.Web.UI.RadToolBarSplitButton();
                newSplitButton.set_enableDefaultButton(false);
                newSplitButton.set_text("Added client-side");
                toolBar.trackChanges();
                toolBar.get_items().add(newSplitButton);
                toolBar.commitChanges();
                return false;
            }

            function getLastItem() {
                var toolBar = $find("<%= RadToolBar1.ClientID %>");
                if (toolBar.get_items().get_count() > 0)
                    return toolBar.get_items().getItem(toolBar.get_items().get_count() - 1);
                return null;
            }

            function removeLastItem() {
                var toolBar = $find("<%= RadToolBar1.ClientID %>");
                if (toolBar.get_items().get_count() > 0) {
                    toolBar.trackChanges();
                    toolBar.get_items().removeAt(toolBar.get_items().get_count() - 1);
                    toolBar.commitChanges();
                }
                return false;
            }

            function enableLastItem() {
                var toolBar = $find("<%= RadToolBar1.ClientID %>");

                var lastItem = getLastItem();
                if (lastItem) {
                    toolBar.trackChanges();
                    lastItem.set_enabled(true);
                    toolBar.commitChanges();
                }
                return false;
            }

            function disableLastItem() {
                var toolBar = $find("<%= RadToolBar1.ClientID %>");
                var lastItem = getLastItem();
                if (lastItem) {
                    toolBar.trackChanges();
                    lastItem.set_enabled(false);
                    toolBar.commitChanges();
                }
                return false;
            }
            //]]>
        </script>

        <qsf:Header runat="server" ID="Header1" NavigationLanguage="C#" />
        <qsf:ConfiguratorPanel runat="server" ID="ConfigurationPanel1" Expanded="true">
            <div style="clear: both; margin: 0px">
                <fieldset style="clear: both; padding-bottom: 10px; padding-left: 10px; width: 500px;">
                    <legend>Add new button</legend>
                    <br />
                    Target:
                    <asp:DropDownList ID="ddlItem" runat="server">
                        <asp:ListItem Text="ToolBar" />
                        <asp:ListItem Text="Last DropDown" />
                        <asp:ListItem Text="Last SplitButton" />
                    </asp:DropDownList>
                    Checked state:
                    <asp:CheckBox ID="cbChecked" runat="server" />
                    <button class="qsfButtonBigger" onclick="return addButton(false);">
                        Add</button>
                </fieldset>
                <fieldset style="float: left; margin-top: 20px; padding-left: 5px; padding-bottom: 5px;
                    padding-right: 5px;">
                    <legend>Add other items</legend>
                    <button class="qsfButtonBigger" onclick="return addDropDown();">
                        Add new DropDown</button>
                    <button class="qsfButtonBigger" onclick="return addSplitButton(false);">
                        Add new SplitButton</button>
                </fieldset>
                <fieldset style="float: left; margin-top: 20px; margin-left: 3px; padding-left: 5px;
                    padding-bottom: 5px; padding-right: 5px;">
                    <legend>Remove</legend>
                    <button class="qsfButtonBigger" onclick="return removeLastItem();">
                        Remove last toolbar item</button><br />
                    <br />
                </fieldset>
                <fieldset style="float: left; margin-top: 20px; margin-left: 3px; padding-left: 5px;
                    padding-bottom: 5px; padding-right: 5px;">
                    <legend>Enable/Disable</legend>
                    <button class="qsfButtonBigger" onclick="return enableLastItem();">
                        Enable last toolbar item</button><br />
                    <br />
                    <button class="qsfButtonBigger" onclick="return disableLastItem();">
                        Disable last toolbar item</button><br />
                    <br />
                </fieldset>
    <asp:Button runat="server" ID="Button1" Text="Postback" CssClass="qsfButtonBigger" style="float: right; clear: left;"
    OnClick="Button1_Click" />

            </div>
        </qsf:ConfiguratorPanel>
        <telerik:RadToolBar runat="server" ID="RadToolBar1" EnableRoundedCorners="true" EnableShadows="true" />
        <qsf:EventLogConsole runat="server" ID="EventLogConsole1" Height="250px" />
        <qsf:Footer runat="server" ID="Footer1" />
        </form>
    </body>
    </html>

Get more than expected!

 
 

Take your time to truly experience the power of RadControls for ASP.NET AJAX with a free 60-day trial backed up by Telerik’s unlimited dedicated support.

Download your RadControls for ASP.NET AJAX trial and jumpstart your development with the available Getting Started resources.

If you have any questions, do not hesitate to contact us at sales@telerik.com.

Copyright 2002-2024 © Telerik. All right reserved
Telerik Inc, 201 Jones Rd, Waltham, MA 02451