Telerik Context Menu With Check-Uncheck

Introduction

 
In this article, we will see how to work with a Telerik context menu control with Check-Uncheck items. In many cases, we need to show menu items with Check-Uncheck icons like copy and paste in Windows Forms. This can be done very easily in Windows applications but in web applications it is difficult. Telerik has provided a mind-blowing control to do it very easily; the RadContextMenu control.
 
To create the Context menu you need to use the steps given below. So let's start working with Telerik RadContextMenu.
 
Step 1: First drag and drop the Telerik Toolbar from the toolbox on your webform, as well as pull the RadToolBarDropDown as an item on the ToolBar for displaying the popup menu when clicking the dropdown.
  1. <telerik:RadToolBar ID="RadToolBar1" runat="server" OnClientDropDownOpening="OnClientDropDownOpening">  
  2.      <Items>  
  3.           <telerik:RadToolBarDropDown runat="server" Text="Take Action">  
  4.           </telerik:RadToolBarDropDown>  
  5.      </Items>  
  6. </telerik:RadToolBar> 
In the preceding code the markup contains one ToolBar and only one item i.e. RadToolBarDropDown. Next on the ToolBar markup we have the OnClientDropDownOpening event; this event fires on the client side when the user clicks on the DropDown item on the ToolBar. Later we will see the method we called.
 
Step 2: Next pull the RadContextMenu from the toolbox with certain menu items in it like the following. Here I'm taking four menu items; that two are main menu items and two are subitems for the second main menu item.
  1. <telerik:RadContextMenu ID="RadContextMenu1" runat="server" OnClientItemClicked="ContextMenuItemClickedOnClient" OnItemClick="RadMenuItem_Click">  
  2.      <Items>  
  3.           <telerik:RadMenuItem runat="server" Text="Item1" Value="1" PostBack="false"></telerik:RadMenuItem>  
  4.           <telerik:RadMenuItem IsSeparator="true"></telerik:RadMenuItem>  
  5.           <telerik:RadMenuItem runat="server" Text="Item2" PostBack="false" Value="2">  
  6.                <Items>  
  7.                     <telerik:RadMenuItem runat="server" Text="SubItem1" Value="S1" PostBack="false"></telerik:RadMenuItem>  
  8.                     <telerik:RadMenuItem IsSeparator="true"></telerik:RadMenuItem>  
  9.                     <telerik:RadMenuItem runat="server" Text="SubMenu2" Value="S2" PostBack="true"></telerik:RadMenuItem>  
  10.                </Items>  
  11.           </telerik:RadMenuItem>  
  12.      </Items>  
  13. </telerik:RadContextMenu> 
In the preceding markup we have created RadContextMenu with some items. In the preceding markup one interesting thing to see is the RadContextMenu has both events, client side item clicked events as well as server side item clicked events. Here we will use both to do something on the client side and something on the server side.
 
Step 3 : Next create some hidden fields to store the state of the RadContextMenu menu items like the following. Here I've taken four items for four menu items.
  1. <input type="hidden" id="hid1" runat="server" value="False" />  
  2. <input type="hidden" id="hid2" runat="server" value="False" />  
  3. <input type="hidden" id="hid3" runat="server" value="False" />  
  4. <input type="hidden" id="hid4" runat="server" value="False" /> 
Step 4: Now we are ready with the design but our main purpose of this article to check the item on click which is done with the following JavaScript method.
  1. function ContextMenuItemClickedOnClient(sender, args) {    
  2.       var menu = $find("<%= RadContextMenu1.ClientID %>");    
  3.       var itemValue = args.get_item().get_value();    
  4.       if (itemValue == "1") {    
  5.             var stateofitem = $get("<%=hid1.ClientID %>").value;    
  6.             if (stateofitem == "False") {    
  7.                    var item = menu.findItemByValue("1");    
  8.                    item.set_imageUrl("checked.gif");    
  9.                    $get("<%=hid1.ClientID %>").value = "True";    
  10.             }    
  11.             else {    
  12.                    var item = menu.findItemByValue("1");    
  13.                    item.set_imageUrl("");    
  14.                    $get("<%=hid1.ClientID %>").value = "False";    
  15.             }    
  16.       }    
  17.       else if (itemValue == "2") {    
  18.              var stateofitem = $get("<%=hid2.ClientID %>").value;    
  19.              if (stateofitem == "False") {    
  20.                     var item = menu.findItemByValue("2");    
  21.                     item.set_imageUrl("checked.gif");    
  22.                     $get("<%=hid2.ClientID %>").value = "True";    
  23.              }    
  24.              else {    
  25.                     var item = menu.findItemByValue("2");    
  26.                     item.set_imageUrl("");    
  27.                     $get("<%=hid2.ClientID %>").value = "False";    
  28.              }    
  29.       }    
  30.       else if (itemValue == "S1") {    
  31.              var stateofitem = $get("<%=hid3.ClientID %>").value;    
  32.              if (stateofitem == "False") {    
  33.                     var item = menu.findItemByValue("S1");    
  34.                     item.set_imageUrl("checked.gif");    
  35.                     $get("<%=hid3.ClientID %>").value = "True";    
  36.              }    
  37.              else {    
  38.                     var item = menu.findItemByValue("S1");    
  39.                     item.set_imageUrl("");    
  40.                     $get("<%=hid3.ClientID %>").value = "False";    
  41.              }    
  42.       }    
  43. }   
The preceding JavaScript method will be called when the user clicks the menuitem where we are getting the MenuitemValue which has been clicked by the user and after checking the value we are setting the image URL of an item.
 
Step 5 : In Step 1 we left the ToolBar DropDownopening event. Next write the following script to open our RadContextMenu on clicking the dropdown of the toolbar:
  1. function OnClientDropDownOpening(sender, eventArgs) {  
  2.        var x = eventArgs.get_item().get_element().offsetLeft + 0;  
  3.        var y = eventArgs.get_item().get_element().offsetTop + 27;  
  4.        showMenuAt(eventArgs.get_domEvent(), x, y);   
  5. }  
  6. function showMenuAt(e, x, y) {  
  7.        var contextMenu = $find("<%= RadContextMenu1.ClientID %>");   
  8.        if (isNaN(x) || isNaN(y)) {  
  9.            alert("Please provide valid integer coordinates");  
  10.            return;  
  11.        }  
  12.        contextMenu.showAt(x, y);  
  13.        $telerik.cancelRawEvent(e);   
Step 6: Next write the following code in the page load event to maintain the state of menuitems on postbacks:
  1. RadMenuItem _item1 = (RadMenuItem)RadContextMenu1.FindItemByValue("1");  
  2. RadMenuItem _item2 = (RadMenuItem)RadContextMenu1.FindItemByValue("2");  
  3. RadMenuItem _subitem1 = (RadMenuItem)RadContextMenu1.FindItemByValue("S1");  
  4. bool _itemonevalue = Convert.ToBoolean(hid1.Value);  
  5. bool _itemtwovalue = Convert.ToBoolean(hid2.Value);  
  6. bool _subitemonevalue = Convert.ToBoolean(hid3.Value);  
  7. if (_itemonevalue)  
  8. {  
  9.       _item1.ImageUrl = "~/checked.gif";  
  10.        hid1.Value = "True";  
  11. }  
  12. if (_itemtwovalue)  
  13. {  
  14.        _item2.ImageUrl = "~/checked.gif";  
  15.         hid2.Value = "True";  
  16. }  
  17. if (_subitemonevalue)  
  18. {  
  19.       _subitem1.ImageUrl = "~/checked.gif";  
  20.         hid3.Value = "True";  
In the preceding code, we are checking the values of hidden fields and taking action to check the menuitems.
 
Step 7: Next write the server-side event for the RadContextMenu to do some useful tasks like the following:
  1. protected void RadMenuItem_Click(object sender, RadMenuEventArgs e)  
  2. {  
  3.      //do Server Side Activity Here  
  4.      RadMenuItem _item = e.Item;  
  5.      bool _s2 = Convert.ToBoolean(hid4.Value);  
  6.      if (!_s2)  
  7.      {  
  8.          _item.ImageUrl = "~/checked.gif";  
  9.           hid4.Value = "True";  
  10.       }  
  11.       else  
  12.       {  
  13.           _item.ImageUrl = "";  
  14.           hid4.Value = "False";  
  15.       }  
Step 8: Next run your application and see the output with the radcontextmenu with Check-Uncheck menuitems.
 

Conclusion

 
In this easy way, we can create attractive menus like Windows Forms menus for web forms using Telerik.