radgrid and contextmenu

4 posts, 0 answers
  1. Robert Smit
    Robert Smit avatar
    13 posts
    Member since:
    Aug 2009

    Posted 09 Oct 2009 Link to this post

    Dear Telerik,

    I would like to use the (context)menu in a radgrid. However I wish to show the context menu when the user hovers over an edit-image (no right click) inside the grid. I need a postback to do some serverwork and I need the value of the DataKeyName of the rowindex clicked.

    What is the best approach? Is there an example/demo online that uses this technique?

    Best wishes
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 12 Oct 2009 Link to this post

    Hi Robert,

    I tried following approach in order to achieve the functionality. I tried attaching OnMouseHover event to Edit image and pass the index and event as parameter. In the event handler, save the index in a  HiddelField and show th emenu. And in the ItemClick server event, get the clicked rowindex from the HiddenFeld.

    ASPX:
     
     
    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1"  
    AllowAutomaticDeletes="True" AllowAutomaticInserts="True" > 
    <MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1"    
    </MasterTableView> 
    </telerik:RadGrid> 
     
    <telerik:RadContextMenu ID="RadMenu1" runat="server" OnItemClick="RadMenu1_ItemClick"
        <Items> 
         . . . 
        </Items>    
    </telerik:RadContextMenu> 
    <asp:HiddenField ID="HiddenField1" runat="server" /> 

    JavaScript:
     
    <script type="text/javascript"
        function contextmenu(index, e) { 
            var menu = $find("<%=RadMenu1.ClientID %>"); 
            document.getElementById("HiddenField1").value = index; 
            menu.show(e); 
        } 
    </script> 

    C#:
     
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem item = (GridDataItem)e.Item; 
             
            ImageButton imageButtton = (ImageButton)item["EditCommandColumn"].Controls[0]; 
            imageButtton.Attributes.Add("onMouseOver""contextmenu('" + e.Item.ItemIndex + "',event);"); // Attach onmousehover attribute 
        } 
    protected void RadMenu1_ItemClick(object sender, RadMenuEventArgs e) 
    {         
        Response.Write(HiddenField1.Value.ToString()); // Get the rowindex 

    -Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Robert Smit
    Robert Smit avatar
    13 posts
    Member since:
    Aug 2009

    Posted 14 Oct 2009 Link to this post

    Hi Shinu,

    Thanks a lot for this solution.

    The only small concern I have is the fact that the ID is being exposed through the Hiddenfield. Isn't it possible to use the DateKeyName which is stored in the ViewState? Or might you have any other other solution to solve this security issue?

    Thanks a lot in advance.

    Best wishes,
    Robert.
  5. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 19 Oct 2009 Link to this post

    Hello Robert,

    You can also use client key/values (along with ClientDataKeyNames) to fetch the id of the hovered grid item as demonstrated on this online demo of the product:

    http://demos.telerik.com/aspnet-ajax/grid/examples/client/keys/defaultcs.aspx

    Kind regards,
    Sebastian
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top