Click here to Skip to main content
13,152,321 members (39,141 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 29 Mar 2013

Example of gridview rowcommand on Button Click

, 29 Mar 2013
Rate this:
Please Sign up or sign in to vote.
Example For Gridview RowCommand Event


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.


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:GridView ID="gridMembersList" 

AutoGenerateColumns="False" GridLines="None" 


        <asp:TemplateField HeaderText="User Name">
            <asp:Literal ID="ltrlName" runat="server" 

            Text='<%# Eval("Name") %>'></asp:Literal>
            <asp:Literal ID="ltrlSlno" runat="server" Visible="False" 

                Text='<%# Eval("Id") %>'></asp:Literal>
        <asp:TemplateField HeaderText="View More">
            <asp:Button ID="btnViewmore"  

            CommandArgument="<%# ((GridViewRow) Container).RowIndex %>
            " CommandName="More" runat="server" Text="View More" />

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.

protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            string pathofxml = Server.MapPath("xml/MemberDetails.xml");
            DataSet ds = new DataSet();
            gridMembersList.DataSource = ds;
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.


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


About the Author

Nandakishore G N
Software Developer Canarys Automation Pvt Ltd
India India
Software Engineer in Canarys Automation Pvt Ltd, Bangalore, India.

My Blog

You may also be interested in...

Comments and Discussions

QuestionPossible to start with expanded view? Pin
surfdabbler19-Apr-15 19:57
membersurfdabbler19-Apr-15 19:57 
QuestionCommandArgument is incorrect Pin
docdaven18-Mar-15 15:56
memberdocdaven18-Mar-15 15:56 
AnswerRe: CommandArgument is incorrect Pin
docdaven18-Mar-15 15:59
memberdocdaven18-Mar-15 15:59 
GeneralMy vote of 5 Pin
Pradeepkumar patil sonu3-Mar-15 0:20
memberPradeepkumar patil sonu3-Mar-15 0:20 
QuestionThanks for this line of code Pin
codeproject_sud4-Jan-15 23:53
membercodeproject_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
memberCityHunter8912-May-14 21:08 
QuestionRe: Pin
ethn11-Mar-14 8:39
memberethn11-Mar-14 8:39 
GeneralMy vote of 5 Pin
Debopam Pal19-Nov-13 13:03
memberDebopam Pal19-Nov-13 13:03 
GeneralRe: My vote of 5 Pin
Nandakishorerao19-Nov-13 19:01
professionalNandakishorerao19-Nov-13 19:01 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170924.2 | Last Updated 30 Mar 2013
Article Copyright 2013 by Nandakishore G N
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid