Introduction
In this article, I am displaying how to make context menu in Gridview with the help of Javascript. Gridview is used to display data in tabular form. We can perform so many operations on Gridview in ASP.Net. We can filter the data as per our requirement, can show columns as per end user requirements. We can show/hide columns at run time according to situation. We can bind Gridview dynamically with database but connectivity with database should be proper. Gridview can bind many databases like MS Access, MS SQL Server, MySQL, XML, SQLlite, Oracle etc.Context Menu Benefits
Context Menu provides a rich user experience, with the help of Context Menu user can quick operation with help of mouse, can perform operations enriched in Context Menu. In this article, Context Menu is embedded into ASP.net Server side control in Gridview.To implement Context Menu in asp.net, please try this code on RowDataBound event of Gridview. Please copy this code and paste in Codebehind class:
Vb.Net Code for Context menu
Protected Sub Grd_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Grd.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then e.Row.Attributes.Add("oncontextmenu", "return ShowMenu(" + e.Row.RowIndex.ToString() + ")") RowIndex = e.Row.RowIndex End If End Sub
C# Code for Context Menu
protected void Grd_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("oncontextmenu", "return ShowMenu(" + e.Row.RowIndex.ToString() + ")"); RowIndex = e.Row.RowIndex; } }
Thanks Anjan , I am beginner, I wonder how to créate "menú option" , and how to call code behind function when click any "menú option".
ReplyDeleteI am working with C#.
Thanks
Joel
Hi Anjan.
ReplyDeleteI am trying to capture the value of "ZIP" (only) using Gridview. Bellow is the code that i have now. Any thoughts ? thank you.
Dim cnndb As New SqlClient.SqlConnection
cnndb.ConnectionString = "xxxxxx yyyyyyy vvvvv hhhhhh"
cnndb.Open()
Dim da As New SqlClient.SqlDataAdapter("select customer, ZIP,country from Client_DB where country='England' ", cnndb)
Dim MyDataSet As New DataSet("mylinedata")
da.Fill(MyDataSet, "mytable")
GridView1.DataSource = MyDataSet.Tables(0)
GridView1.DataBind()
On RowDataBound we can pick cell value, here is code:
ReplyDeleteprotected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string name = e.Row.Cells[0].Text;
}
}
Nice, If you want you can see shieldui contextmenu - it`s very easy to use
ReplyDeletehttps://demos.shieldui.com/aspnet/contextmenu/basic-usage