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

Dynamically Generate RadContextMenu from SQL Server Table using Cell Value of the RadGrid (on which the user clicked to get the Context Menu) as a parameter

1 Answer 63 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nishant
Top achievements
Rank 1
Nishant asked on 02 Jul 2012, 11:40 PM
Hi,

I'm trying to Dynamically generate the RadContextMenu based on the contents of the Cell in RadGrid on which the user clicks. The menu items are stored in the SQL Sever database. Below is my client side code :

function RowContextMenu(sender, eventArgs) {
                var evt = eventArgs.get_domEvent();
  
                if (evt.target.tagName == "INPUT" || evt.target.tagName == "A") {
                    return;
                }
  
                var index = eventArgs.get_itemIndexHierarchical();
                var row = sender.get_masterTableView().get_dataItems()[index];
  
                var current_status = sender.get_masterTableView().getCellByColumnUniqueName(row, "Current_Status").innerHTML;
                var process_status = sender.get_masterTableView().getCellByColumnUniqueName(row, sender.get_masterTableView().get_columns()[evt.target.cellIndex].get_uniqueName()).innerHTML;
                var process_area = //write code to get the column header of the selected cell
  
                document.getElementById("radGridClickedRowIndex").value = index;
                sender.get_masterTableView().selectItem(row.get_element(), true);
  
                var menu = $find("<%=RadMenu1.ClientID %>");
                menu.show(evt);
  
                evt.cancelBubble = true;
                evt.returnValue = false;
  
                if (evt.stopPropagation) {
                    evt.stopPropagation();
                    evt.preventDefault();
                }
            }

Now i want to pass the values current_Status, process_status and process_area to SQL server and get the list of actions to populate in the context menu.

Below are some snippets of the ASPX code:
<asp:SqlDataSource ID="ActionsDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:MetadataCS %>"
    ProviderName="<%$ ConnectionStrings:MetadataCS.ProviderName %>" SelectCommand="SELECT [Action] FROM [FileStatusActions] WHERE (([Process_Area] = @Process_Area) AND ([Current_Status] = @Current_Status) AND ([Process_Status] = @Process_Status))">
    <SelectParameters>
        <asp:Parameter Name="Process_Area" Type="String" />
        <asp:Parameter Name="Current_Status" Type="String" />
        <asp:Parameter Name="Process_Status" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>
<telerik:RadContextMenu ID="RadMenu1" runat="server" OnItemClick="RadMenu1_ItemClick" OnClientShowing="showContextMenu"
    DataSourceID="ActionsDataSource" DataTextField="Action" DataValueField="Action" AppendDataBoundItems="true"
    EnableRoundedCorners="true" EnableShadows="true">
    <Items>
        <telerik:RadMenuItem Text="Dynamic Menu" />
    </Items>
</telerik:RadContextMenu>

Please help me achieve the desired functionality using either client side/server side code ? I am also willing to use XML files to load menu instead of database if feasible/possible.

Thanks,
Nishant

1 Answer, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 05 Jul 2012, 11:11 AM
Hello Nishant,

If you have the information only on the server, you would need to add the items server-side. Try the following:

1) Wrap the RadGrid in a RadAjaxPanel together with the RadContextMenu OR use a RadAjaxManager to update the grid and the menu.
2) Wire the client-side OnRowContextMenu event of RadGrid, collect the needed information and pass it to the server using the ajaxRequest method of the respective RadAjax control.
3) On the server, use the passed information to populate the menu and register a client script block that will show the menu.

Greetings,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Nishant
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Share this question
or