Thursday, September 13, 2007

Context Menu in Asp.Net 2.0 GridView

Right - Click Context Menu in Asp.Net 2.0 GridView

You can download the dll as well as sample for context menu from http://msdn.microsoft.com/msdnmag/issues/05/02/CuttingEdge/
Just include the contextmenu.dll in your toolbox and add context menu control.

To Add menu items as follows,
<cc1:contextmenu id="ContextMenu1" runat="server" autohide="False">
<contextmenuitems>
<cc1:contextmenuitem commandname="Copy" text="Copy" tooltip="Click to Copy">
<cc1:contextmenuitem commandname="Paste" text="Paste" tooltip="Click to Paste">
</contextmenuitems>
</cc1:ContextMenu>

In Your Page Html script include the following code in body,
onkeypress="<% = ContextMenu1.GetEscReference() %>"
onclick="<% = ContextMenu1.GetOnClickReference() %>"


The above code is used to hide the context menu while Escape button was clicking as well as left clicking anywhere in the page.

For Button, Add this attribute in button script
oncontextmenu ="<% = ContextMenu1.GetMenuReference() %>"

For Grid-View Row, You should add script funtion ,
<script language="javascript" type="text/javascript">
function CallContextMenu()
{
<% = ContextMenu1.GetMenuReference()%>
}
</script>

In Code-Behind , Add the following code on RowDataBound Event,
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("oncontextmenu", "javascript:ff();return false;");
}

Now we can get our own context menu in Gridview control.


No comments: