Dynamically generating items in a RadContextMenu

4 posts, 0 answers
  1. SHD
    SHD avatar
    19 posts
    Member since:
    Apr 2007

    Posted 29 Dec 2008 Link to this post

    I have a RadContextMenu bound to a RadGrid. The items that appear in the RadContextMenu are not constant for each row in the RadGrid (for example, menu options for Row 1 are Edit, Delete, for Row 2 are Edit, Checkout, Delete, menu option for Row 3 is Publish, and so on).

    When the right click event is fired for a row in the RadGrid, I want to make a server side call to a function that determines what items can appear in the context menu for that row based on the state of that row. Can you please direct me to a sample to acheive the same?

    Thank you for your help. 
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 30 Dec 2008 Link to this post

     Hi,

    One suggestion is that you can check particular value of the Grid cell filtered by the ColumnUninqueName. Then according to that value, it is possible to add the MenuItems as your requirement from client side. I have tried this example by using the Column "ContactTitle" of Northwind database for checking the Grid cell value. I have tried to add MenuItem "Publish" if ContactTitle is "Sales Representative" and adds MenuItem "CheckOut" if the ContactTitle is "Marketing Manager". I hope this will help you.

    JavaScript:
    <script type="text/javascript">    
    function RowContextMenu(sender, eventArgs) 
       var MasterTable = sender.get_masterTableView(); 
       var row = MasterTable.get_dataItems()[eventArgs.get_itemIndexHierarchical()]; 
       var cell = MasterTable.getCellByColumnUniqueName(row, "ContactTitle"); 
       var menu = $find("RadContextMenu1"); 
       if(cell.innerText== 'Sales Representative'
              additems("Publish"); 
       else if (cell.innerText=='Marketing Manager'
              additems("CheckOut"); 
       else if(cell.innerText!='Marketing Manager' || cell.innerText != 'Sales Representative')     
            additems(null);   
    function additems(str) 
        var menu = $find("RadContextMenu1"); 
        menu.trackChanges(); 
        menu.get_items().clear(); 
        var childItem1 = new Telerik.Web.UI.RadMenuItem(); 
        childItem1.set_text("Edit"); 
        menu.get_items().add(childItem1); 
        var childItem2 = new Telerik.Web.UI.RadMenuItem(); 
        childItem2.set_text("Delete");   
        menu.get_items().add(childItem2);       
     
        if (str=='Publish' && !menu._findItemByText('Publish')) 
        { 
            var childItem = new Telerik.Web.UI.RadMenuItem(); 
            childItem.set_text("Publish"); 
            menu.get_items().add(childItem);         
        } 
        else if(str=='CheckOut' && !menu._findItemByText('CheckOut')) 
        { 
            var childItem = new Telerik.Web.UI.RadMenuItem(); 
            childItem.set_text("CheckOut"); 
            menu.get_items().add(childItem);         
        } 
        menu.commitChanges(); 
    </script> 

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. SHD
    SHD avatar
    19 posts
    Member since:
    Apr 2007

    Posted 30 Dec 2008 Link to this post

    Shinu, thank you for your reply.

    I looked at your sample. My function that determines the options in the context menu based on row state is a server side C# function. Is there a way to acheive the same using server side code instead of client side API?
    Alternatively, is there a way to make a webservice call to a function that determines the menu options? Then in a client side function I can hide the menu options that are not valid for that row based on the values returned by the webservice call.

    Thank you for your help.
  5. SHD
    SHD avatar
    19 posts
    Member since:
    Apr 2007

    Posted 31 Dec 2008 Link to this post

    I found this sample http://demos.telerik.com/aspnet-ajax/Menu/Examples/Programming/WebService/DefaultCS.aspx for dynamically loading child RadMenuItems items in a RadMenu using a web service. Is there something similar I can use to dynamically populate items in a RadContextMenu of a RadGrid using a web service? As I mentioned earlier, the items that I need to show are determined using some server side logic on a per row basis.

    Thank you in advance for your help.
Back to Top