This is a migrated thread and some comments may be shown as answers.

Dynamically generating items in a RadContextMenu

3 Answers 365 Views
Menu
This is a migrated thread and some comments may be shown as answers.
SHD
Top achievements
Rank 1
SHD asked on 29 Dec 2008, 04:36 PM
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. 

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 30 Dec 2008, 12:07 PM

 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.
0
SHD
Top achievements
Rank 1
answered on 30 Dec 2008, 02:38 PM
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.
0
SHD
Top achievements
Rank 1
answered on 31 Dec 2008, 02:55 PM
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.
Tags
Menu
Asked by
SHD
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
SHD
Top achievements
Rank 1
Share this question
or