Search This Blog

2009-05-08

How can I enable right click on a control in a aspx webpage

How can I show Context menu or popup menu for web pages/Adding context menu for asp.net control
Context menu or popup menu is a common element of most desktop applications, it is still fairly uncommon in Web application names because it doesn't map well to a server-based technology like ASP.NET.

In this column, I'll create an ASP.NET context menu that addresses the Internet Explorer object model.



Step 1.Download CuttingEdge0502.exe and unzip it

Step 2.copy the ContextMenu.dll from CuttingEdge0502\bin folder and paste it to your bin directory of your web application

Step 3.In your aspx page write the following code in the Register Directive


<%@ Register TagPrefix="cc1" Namespace="Msdn" Assembly="ContextMenu" %>



Step 4.Write the following code in the body section of your aspx page

<cc1:ContextMenu ID="ContextMenu2" runat="server" Width="100px" ForeColor="Black" Font-Names="verdana" Font-Size="8pt" AutoHide="False" OnItemCommand="ContextMenu2_ItemCommand" RolloverColor="Desktop">

<ContextMenuItems>

<cc1:ContextMenuItem Text="Optimize" CommandName="Optimize" Tooltip="Optimize">

</cc1:ContextMenuItem>

</ContextMenuItems>

</cc1:ContextMenu>



you can increase /modify the menu item by adding/modifying the ContextMenuItem properties

And write the following code in the HTML Body as follows

<body onkeypress="<% = ContextMenu1.GetEscReference() %>" onclick="<% = ContextMenu1.GetOnClickReference() %>">

Step 5.In your code behind class ,write the following code in the pageload event

ContextMenu2.BoundControls.Add(TreeView2);

Treeview2 is the aspx control on which I want to show the context menu for the right click event



Step 6.Now write a method which will call onclick event of your context menu

protected void ContextMenu2_ItemCommand(object sender, CommandEventArgs e)

{
if (e.CommandName == "Optimize")
{
Response.Write("Hello Manab");
}
}
if you have multiple contextmenu item ,you can track this by e.commandName

Following is the output of my Context menu


Resource:http://msdn.microsoft.com/en-us/magazine/cc163848.aspx

No comments: