RowCommand Event in GridView

This article explains how to use the RowCommand Event in a Gridview.

The RowCommand Event can be used to get the selected GridView Row value or text.

GridView.RowCommand Event:

The RowCommand Event occurs when a button is clicked in a GridView control.

  1. <asp:GridView OnRowCommand="GridViewCommandEventHandler" />
The RowCommand event is raised when a button is clicked in the GridView control. This enables you to provide an event-handling method that performs a custom routine whenever this event occurs.

CommandArgument:

The CommandArgument can contain any string set by the programmer. The CommandArgument property complements the CommandName property by allowing you to provide any additional information for the command. For example, you can set the CommandName property to Sort and set the CommandArgument property to Ascending to specify a command to sort in ascending order.
  1. <asp:LinkButton ID="lnkproductname" runat ="server" CommandArgument='<%#Eval("productid")%> ></asp:LinkButton> 
CommandName:

CommandName property to determine the command to perform. The CommandArgument property complements the CommandName property by allowing you to provide any additional information for the command to perform. For example, you can set the CommandName property to Sort and set the CommandArgument property to Ascending to specify a command to sort in ascending order.
  1. <asp:LinkButton ID="lnkproductname" runat ="server" CommandArgument='<%#Eval("productid")%>' CommandName ="selectproduct"   
  2. Text ='<%#Eval ("productname") %>'>  
  3. </asp:LinkButton>
Below is the Image of GridView after binding the data
 
RowCommand1.gif

In .aspx source code page 

  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">  
  2.   <Columns >  
  3.     <asp:TemplateField HeaderText ="Product Name">  
  4.       <ItemTemplate>  
  5.         <asp:LinkButton ID="lnkproductname" runat ="server" CommandArgument=''  
  6.           <%#Eval("productid")%>  
  7.             ' Text ='<%#Eval("productname") %>' CommandName ="selectproduct" ></asp:LinkButton>  
  8.       </ItemTemplate>  
  9.     </asp:TemplateField>  
  10.     <asp:TemplateField HeaderText ="Vendor Name">  
  11.       <ItemTemplate >  
  12.         <asp:LinkButton ID ="lnkvendorname" runat ="server" CommandArgument=''  
  13.           <%#Eval("vendorid")%>  
  14.             ' CommandName ="selectvendor" Text ='<%#Eval("vendorname") %>'></asp:LinkButton>  
  15.       </ItemTemplate>  
  16.     </asp:TemplateField>  
  17.   
  18.     <asp:TemplateField HeaderText ="City">  
  19.       <ItemTemplate >  
  20.         <asp:Label ID ="lnkcity" runat ="server" Text =''  
  21.           <%#Eval("city") %>'>  
  22.         </asp:Label>  
  23.       </ItemTemplate>  
  24.     </asp:TemplateField>  
  25.   </Columns>  
  26. </asp:GridView> 
Below is the code for getting the selected Row in Gridview using RowCommand Event.

 

  1. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)  
  2. {  
  3.     if (e.CommandName == "selectproduct")  
  4.     {  
  5.         textBox1.Text = Convert.ToString(e.CommandArgument.ToString());  
  6.     }  
  7.   
  8.     if (e.CommandName == "selectvendor")  
  9.     {  
  10.         string vendid = Convert.ToString(e.CommandArgument.ToString());  
  11.         Response.Write("Vendorid:" + vendid);  
  12.     }  
  13. }  
Below is the Image of GridView Seleted Row using RowCommand Event.

RowCommand2.gif

Thanks for reading my article.


Similar Articles