Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET
Tip/Trick

Example of gridview rowcommand on Button Click

Rate me:
Please Sign up or sign in to vote.
4.80/5 (19 votes)
29 Mar 2013CPOL 263.4K   2.6K   14   19
Example For Gridview RowCommand Event

Introduction

One of the most used controls in my projects is Gridview. Therefore, I thought of writing a tip which has been used in my projects frequently.

Background

Gridview displays the value of a data source in a table. Here, I am going to give an example of using an event called "RowCommand". The RowCommand is raised when a button, LinkButton or ImageButton is clicked in the Gridview Control.

Using the Code

The HTML part of the gridview is:

ASP.NET
<asp:GridView ID="gridMembersList" 
AutoGenerateColumns="False" GridLines="None" 
            runat="server"  
            onrowcommand="gridMembersList_RowCommand">
        <Columns>
        <asp:TemplateField HeaderText="User Name">
        <ItemTemplate>
            <asp:Literal ID="ltrlName" runat="server" 
            Text='<%# Eval("Name") %>'></asp:Literal>
            <asp:Literal ID="ltrlSlno" runat="server" Visible="False" 
                Text='<%# Eval("Id") %>'></asp:Literal>
        </ItemTemplate>
        </asp:TemplateField>
        
        <asp:TemplateField HeaderText="View More">
        <ItemTemplate>
            <asp:Button ID="btnViewmore"  
            CommandArgument="<%# ((GridViewRow) Container).RowIndex %>
            " CommandName="More" runat="server" Text="View More" />
        </ItemTemplate>
        </asp:TemplateField> 
        </Columns>
        </asp:GridView>  

In the HTML part , I have binded the values which have to be displayed on the page.

And in the code behind, I have used an XML to load the data to the gridview. When the button is clicked, the event verifies for the command name and command argument.

And, then, I have just alerted the name of the user in this example. Changes can be made according to the requirement.

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string pathofxml = Server.MapPath("xml/MemberDetails.xml");
            DataSet ds = new DataSet();
            ds.ReadXml(pathofxml);
            gridMembersList.DataSource = ds;
            gridMembersList.DataBind();
        }
    } 
C#
protected void gridMembersList_RowCommand(object sender, GridViewCommandEventArgs e)
  {
       if (e.CommandName == "More")
       {
           int index = Convert.ToInt32(e.CommandArgument.ToString());
           Literal ltrlslno = (Literal)gridMembersList.Rows[index].FindControl("ltrlSlno");
           Literal ltrlName = (Literal)gridMembersList.Rows[index].FindControl("ltrlName");
           ScriptManager.RegisterStartupScript(this, this.GetType(),
           "Message", "alert('" + ltrlName.Text+ "');", true);
       }
   }

I hope this will be helpful for the beginner.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Dhruv Compusoft Consultancy Pvt Ltd
India India
Senior MES Developer in TruGlobal Pvt Ltd, Bangalore, India.

My Blog
dotnetkraft.blogspot.in
gnnrao.blogspot.in

Comments and Discussions

 
QuestionPossible to start with expanded view? Pin
surfdabbler19-Apr-15 19:57
surfdabbler19-Apr-15 19:57 
QuestionCommandArgument is incorrect Pin
docdaven18-Mar-15 15:56
docdaven18-Mar-15 15:56 
AnswerRe: CommandArgument is incorrect Pin
docdaven18-Mar-15 15:59
docdaven18-Mar-15 15:59 
GeneralMy vote of 5 Pin
Pradeepkumar patil sonu3-Mar-15 0:20
Pradeepkumar patil sonu3-Mar-15 0:20 
QuestionThanks for this line of code Pin
codeproject_sud4-Jan-15 23:53
codeproject_sud4-Jan-15 23:53 
AnswerRe: Thanks for this line of code Pin
Nandakishore G N5-Jan-15 19:00
professionalNandakishore G N5-Jan-15 19:00 
GeneralMy vote of 4 Pin
CityHunter8912-May-14 21:08
CityHunter8912-May-14 21:08 
QuestionRe: Pin
ethn11-Mar-14 8:39
ethn11-Mar-14 8:39 
GeneralMy vote of 5 Pin
Debopam Pal19-Nov-13 13:03
professionalDebopam Pal19-Nov-13 13:03 
GeneralRe: My vote of 5 Pin
Nandakishore G N19-Nov-13 19:01
professionalNandakishore G N19-Nov-13 19:01 
Questionwith more buttons ? Pin
Member 1036091225-Oct-13 12:55
Member 1036091225-Oct-13 12:55 
AnswerRe: with more buttons ? Pin
Nandakishore G N27-Oct-13 19:39
professionalNandakishore G N27-Oct-13 19:39 
QuestionVery nice one Pin
Member 433445817-Oct-13 7:49
Member 433445817-Oct-13 7:49 
AnswerRe: Very nice one Pin
Nandakishore G N17-Oct-13 23:20
professionalNandakishore G N17-Oct-13 23:20 
GeneralMy vote of 4 Pin
Alireza_136229-Jul-13 21:59
Alireza_136229-Jul-13 21:59 
GeneralRe: My vote of 4 Pin
Nandakishore G N29-Jul-13 23:36
professionalNandakishore G N29-Jul-13 23:36 
GeneralRe: My vote of 4 Pin
Debopam Pal19-Nov-13 12:59
professionalDebopam Pal19-Nov-13 12:59 
GeneralRe: My vote of 4 Pin
Nandakishore G N19-Nov-13 18:03
professionalNandakishore G N19-Nov-13 18:03 
GeneralRe: My vote of 4 Pin
Debopam Pal19-Nov-13 20:42
professionalDebopam Pal19-Nov-13 20:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.