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

radgrid and contextmenu

3 Answers 84 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Robert Smit
Top achievements
Rank 1
Robert Smit asked on 09 Oct 2009, 03:24 PM
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

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 12 Oct 2009, 12:25 PM
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.
0
Robert Smit
Top achievements
Rank 1
answered on 14 Oct 2009, 03:26 PM
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.
0
Sebastian
Telerik team
answered on 19 Oct 2009, 11:40 AM
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.
Tags
Grid
Asked by
Robert Smit
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Robert Smit
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or