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:
var toolBar = $find("RadToolBar1"); toolBar.trackChanges(); //add, remove, disable items toolBar.commitChanges();
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; } }
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="DefaultVB.aspx.vb" Inherits="Telerik.Web.Examples.ToolBar.ClientSide.AddRemoveDisableItems.DefaultVB" %><%@ 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="VB" /> <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>
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.