This blog is all about to creating GridView
with Context Menu in ASP.NET.
Step 1: .aspx page
<%@
Page Language="C#"
AutoEventWireup="true"
CodeBehind="ContextMenu.aspx.cs"
Inherits="ContextMenu.ContextMenu"
%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml">
<head
runat="server">
<title>Grid
View Demo</title>
<link
href="_assets/css/StyleSheet.css"
rel="stylesheet"
type="text/css"
/>
<link
href="_assets/css/confirm.css"
rel="stylesheet"
type="text/css"
/>
<link
href="_assets/css/jquery.contextMenu.css"
rel="stylesheet"
type="text/css"
/>
<script
src="_assets/js/jquery-1.2.6.min.js"
type="text/javascript"></script>
<script
src="_assets/js/jquery.simplemodal-1.1.1.js"
type="text/javascript"></script>
<script
src="_assets/js/jquery.contextMenu.js"
type="text/javascript"></script>
<!-- IE 6 hacks -->
<!--[if lt IE 7]>
<link type='text/css' href='_assets/css/confirm_ie.css' rel='stylesheet'
media='screen' />
<![endif]-->
<style
type="text/css">
.customerRow
{
}
.gvhide
{
display:
none;
}
</style>
<script
language="javascript"
type="text/javascript">
var
CustomerID = null;
var CompanyName =
null;
var ContactName =
null;
$(document).ready(function () {
$(".customerRow").contextMenu({
menu: 'myMenu' },
function (action, el, pos) { contextMenuWork(action, el, pos); });
$(".openmenu").contextMenu({ menu:
'myMenu', leftButton:
true }, function (action, el, pos) {
contextMenuWork(action, el.parent("tr"), pos);
});
});
function contextMenuWork(action, el,
pos) {
var rowindex = (el[0].rowIndex * 1 -
1);
CustomerID = $("#Gridview1_lbl_CustomerID_"
+ rowindex).html();
CompanyName = $("#Gridview1_lbl_CompanyName_"
+ rowindex).html();
ContactName = $("#Gridview1_lbl_ContactName_"
+ rowindex).html();
switch (action) {
case
"delete":
{
var msg = "Delete " + ContactName
+ "?";
confirm(msg);
break;
}
case
"insert":
{
alert("Insert");
break;
}
case "edit":
{
alert("Edit");
break;
}
}
}
function pageLoad() {
$(".customerRow").contextMenu({
menu: 'myMenu' },
function (action, el, pos) { contextMenuWork(action, el, pos); });
$(".openmenu").contextMenu({ menu:
'myMenu', leftButton:
true }, function (action, el, pos) {
contextMenuWork(action, el.parent("tr"), pos);
});
}
</script>
</head>
<body>
<form
id="form1"
runat="server">
<asp:ScriptManager
ID="ScriptManager1"
runat="server">
</asp:ScriptManager>
<asp:UpdatePanel
ID="UpdatePanel1"
runat="server">
<ContentTemplate>
<asp:GridView
ID="Gridview1"
runat="server"
AutoGenerateColumns="false"
OnRowDataBound="Gridview1_RowDataBound"
AllowPaging="true"
OnPageIndexChanging="Gridview1_PageIndexChanging"
PageSize="5">
<Columns>
<asp:TemplateField
HeaderText="CustomerID">
<ItemTemplate>
<asp:Label
ID="lbl_CustomerID"
runat="server"
Text='<%#
Eval("customerid") %>'></asp:Label>
</ItemTemplate>
<ItemStyle
CssClass="gvhide"
/>
<HeaderStyle
CssClass="gvhide"
/>
</asp:TemplateField>
<asp:TemplateField
HeaderText="CompanyName">
<ItemTemplate>
<asp:Label
ID="lbl_CompanyName"
runat="server"
Text='<%#
Eval("CompanyName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField
HeaderText="ContactName">
<ItemTemplate>
<asp:Label
ID="lbl_ContactName"
runat="server"
Text='<%#
Eval("ContactName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.


Chris RandlePosted Feb 20, 2019, 2:17 AM
Https://www.codeproject.com/Articles/63902/Right-Click-Menu-using-JQuery-ASP-NET-using-C
Chris RandlePosted Feb 20, 2019, 2:08 AM
Why does this code look the same as this guy's code (published 3 months earlier)? https://chinmayaparija.blogspot.com/2013/03/context-menu-in-using-jquery.html
Aleksandra NikolovaPosted Sep 16, 2016, 1:48 AM
Nice, you can see also shieldui contextmenu https://demos.shieldui.com/aspnet/contextmenu/basic-usage
jyoti yadavPosted Jun 7, 2016, 8:49 PM
how can we write code for insert/del/update in the above application
Raghava ReddyPosted Apr 20, 2015, 2:22 AM
add xml code and jquery plugins for this..
ElmarPosted Nov 14, 2014, 2:45 PM
how to disable the contextmenu from codebehind? I have almost everything like in your example: $(".GridviewRow").contextMenu({ menu: 'myMenu' }, function (action, el, pos) { contextMenuWork(action, el, pos); }); function disableContextMenu() { $('.GridviewRow).disableContextMenu(); } and in code behind Page.ClientScript.RegisterStartupScript(this.GetType(), "script", "disableContextMenu();", true); but menu is still there?
Prasant BhardwajPosted Oct 14, 2014, 3:36 AM
thanks
Jo LoPosted Jan 14, 2014, 11:02 AM
Thanks, is great!!How can i call codebehind from menu option?
Ajendra PrasadPosted May 6, 2013, 3:21 AM
this is so helpful. thanks a lot :)