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

Menu / Add/Remove/Disable Items

Event log:

  • With RadMenu for ASP.NET Ajax 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 items upon clicking on existing items.

    In the OnClientItemClicked event:

    • Two parameters are passed to this event
      - menu - the menu instance firing the event
      - args - event arguments
    • To get the clicked item we used the event arguments passed to the event:
         var item = args.get_item();
    • To add a new item to the root item:
         //Creates a new item
    
     var childItem = new Telerik.Web.UI.RadMenuItem(); 
     childItem.set_text("New" + item.get_text());

     //Adds the newly created item to the items collection of the clicked item's parent 
     item.get_parent().get_items().add(childItem);
    • To remove an existing item:  
         var parentItem = item.get_parent();
         //Removes the item from the items collection of the clicked item's parent 
         parentItem.get_items().remove(item); 
    • To disable an existing item:
         item.disable(); 
    As you can notice in the above snippets the following client-side methods are used to add/remove/disable items:
    • add(item)  - adds a new item
    • remove(item) - remove an existing item
    • item.disable() - disables an existing item

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

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

Source Code

C# VB.NET
Show code in new window Demo isolation steps
  • <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <%@ 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" %>

    <%@ Page CodeFile="DefaultCS.aspx.cs" Language="c#" AutoEventWireup="true" Inherits="Telerik.Web.Examples.Menu.AddRemoveItemsClientSide.DefaultCS" %>

    <!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 ID="Headtag1" runat="server"></qsf:HeadTag>
        <link rel="stylesheet" href="styles.css" type="text/css" />
    </head>
    <body class="BODY">
        <form id="Form1" runat="server">
            <qsf:Header ID="Header2" runat="server" NavigationLanguage="C#"></qsf:Header>
            <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
            <telerik:RadCodeBlock runat="server" ID="RadCodeBlock1">

                <script type="text/javascript">
                //<![CDATA[
                    var menu;
                    var textBox;
                    
                    function pageLoad()
                    {
                        menu = $find ("<%= RadMenu1.ClientID %>");
                        textBox = $find("<%= RadTextBox1.ClientID %>");
                    }
                    
                    function addItem()
                    {
                        var itemText = textBox.get_value();
                        if (!itemText)
                        {
                            alert("Please specify the text for the new item.");
                            return false;
                        }
                        
                        menu.trackChanges();
                        
                        //Instantiate a new client item
                        var item = new Telerik.Web.UI.RadMenuItem();
                        //Set its text
                        item.set_text(itemText);
                        //Add the new item as the child of the selected item or the menu if no item is selected
                        var parent = menu.get_selectedItem() || menu;
                        parent.get_items().add(item);
                        //Expand the parent if it is not the menu
                        if (parent != menu && !parent.get_isOpen())
                            parent.open();
                        
                        item.set_selected(true);
                        
                        menu.commitChanges();
                    }
                    
                    function deleteItem()
                    {
                        var allItems = menu.get_allItems();
                        if (allItems.length < 1)
                        {
                            alert("The menu is empty.");
                            return false;
                        }

                        var selectedItem = menu.get_selectedItem();
                        if (!selectedItem)
                        {
                            alert("You need to select a item first.");
                            return false;
                        }

                        if (allItems.length == 1)
                        {
                            if (!confirm("This is the last item in the menu. Are you sure you want to delete it?"))
                                return false;
                        }

                        menu.trackChanges();

                        var parent = selectedItem.get_parent();
                        parent.get_items().remove(selectedItem);

                        menu.commitChanges();
                    }

                    function emptyItem()
                    {
                        var allItems = menu.get_allItems();
                        if (allItems.length < 1)
                        {
                            alert("The menu is already empty.");
                            return false;
                        }

                        var selectedItem = menu.get_selectedItem();
                        if (!selectedItem)
                        {
                            alert("You need to select a item first.");
                            return false;
                        }

                        if (allItems.length == 1)
                        {
                            if (!confirm("This is the last item in the menu. Are you sure you want to delete it?"))
                                return false;
                        }

                        menu.trackChanges();

                        var parent = selectedItem.get_items().clear();

                        menu.commitChanges();

                        return false;
                    }

                    function disableItem()
                    {
                        var selectedItem = menu.get_selectedItem();
                        if (!selectedItem)
                        {
                            alert("You need to select a item first.");
                            return false;
                        }
                        
                        menu.trackChanges();
                        
                        selectedItem.disable();
                        
                        menu.commitChanges();
                    }
                    
                    function toggleItem()
                    {
                        var selectedItem = menu.get_selectedItem();
                        if (!selectedItem)
                        {
                            alert("You need to select a item first.");
                            return false;
                        }

                        if (selectedItem.get_isOpen()) {
                            selectedItem.close();
                        } else {
                            selectedItem.open();
                        }
                    }
                    
                    function enableAllItems()
                    {
                        var allItems = menu.get_allItems();
                        for (var i = 0; i < allItems.length; i++)
                        {
                            allItems[i].enable();
                        }
                    }
                //]]>
                </script>

            </telerik:RadCodeBlock>
            <telerik:RadMenu ID="RadMenu1" runat="server" EnableRoundedCorners="true" EnableShadows="true" />
            <div style="overflow: hidden">
             <qsf:ConfiguratorPanel runat="server" ID="ConfigurationPanel1" Title="Available actions" Orientation="Vertical" CssClass="qsfRightConfig" Expanded="true">

                 <ul class="qsfexAvailableActions">
                     <li>
    <asp:Button ID="Button6" CssClass="qsfButtonBig" OnClientClick="toggleItem(); return false;"
    Text="Toggle Selected Item" runat="server" />

                     </li>
                     <li>
    <asp:Button ID="Button4" CssClass="qsfButtonBig" OnClientClick="disableItem(); return false;"
    Text="Disable Selected Item" runat="server" />

                     </li>
                     <li>
    <asp:Button ID="Button7" CssClass="qsfButtonBig" OnClientClick="enableAllItems(); return false;"
    Text="Enable All Items" runat="server" />

                     </li>
                     <li>
    <asp:Button ID="Button3" CssClass="qsfButtonBig" OnClientClick="deleteItem(); return false;"
    Text="Delete Selected Item" runat="server" />

                     </li>
                     <li>
    <asp:Button ID="Button8" CssClass="qsfButtonBig" OnClientClick="emptyItem(); return false;"
    Text="Empty Selected Item" runat="server" />

                     </li>
                     <li>
                         <telerik:RadTextBox runat="server" ID="RadTextBox1" Text="New Item">
    </telerik:RadTextBox><asp:Button ID="Button1" CssClass="qsfButton" OnClientClick="addItem(); return false;"
    Text="Add New Item" runat="server" />

                     </li>
                     <li>
                         <asp:Button ID="Button2" CssClass="qsfButton" Text="Postback" runat="server" OnClick="Button2_Click">
                         </asp:Button>
                     </li>
                 </ul>
             </qsf:ConfiguratorPanel>
            </div>
         <qsf:EventLogConsole runat="server" ID="EventLogConsole1" />
            <qsf:Footer ID="Footer2" runat="server"></qsf:Footer>
        </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