slider

Recent

Navigation

Context menu in Gridview (ASP.NET)

Context menu in Gridview, context menu in gridview c#, context menu in gridview asp net, context menu in gridview

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.

Context menu in Gridview (ASP.NET)

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;
        }
    }

To popup the context menu, paste this code in header of HTML in ASP page


Conclusion

Hope, this article will work to all, how we can create Context Menu in Gridview (ASP.net) server side control. In earlier article, I have explained in detail about Pick Gridview row cell in ASP.Net, Gridview Row Backcolor in ASP.NET, Export Gridview to Excel

Suggested Reading

Share

Anjan Kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

4 comments:

  1. 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".
    I am working with C#.
    Thanks
    Joel

    ReplyDelete
  2. Hi Anjan.
    I 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()

    ReplyDelete
  3. On RowDataBound we can pick cell value, here is code:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    string name = e.Row.Cells[0].Text;
    }
    }

    ReplyDelete
  4. Nice, If you want you can see shieldui contextmenu - it`s very easy to use
    https://demos.shieldui.com/aspnet/contextmenu/basic-usage

    ReplyDelete