Reader Level:
ARTICLE

RowCommand Event in GridView

On February 18, 2011 Articles | ASP.NET Programming
In this Article you will know how to use the RowCommand Event in Gridview
  • 1
  • 0
  • 70272


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.

<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.

<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.

<asp:LinkButton ID="lnkproductname" runat ="server" CommandArgument='<%#Eval("productid")%>' CommandName ="selectproduct"
Text ='<%#Eval ("productname") %>'>


</asp:LinkButton>


Below is the Image of GridView after binding the data

RowCommand1.gif

In .aspx source code page

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">
  <Columns >
    <
asp:TemplateField HeaderText ="Product Name">
      <ItemTemplate>

        <asp:LinkButton ID="lnkproductname" runat ="server" CommandArgument=''
          <%#Eval("productid")%>
            ' Text ='<%#Eval("productname") %>' CommandName ="selectproduct" ></asp:LinkButton
>

      </ItemTemplate>
    </
asp:TemplateField>

    <asp:TemplateField HeaderText ="Vendor Name">
      <ItemTemplate >
        <
asp:LinkButton ID ="lnkvendorname" runat ="server" CommandArgument=''
          <%#Eval("vendorid")%>
            ' CommandName ="selectvendor" Text ='<%#Eval("vendorname") %>'></asp:LinkButton
>
      </ItemTemplate>
    </
asp:TemplateField>

    <asp:TemplateField HeaderText ="City">
      <ItemTemplate >
        <
asp:Label ID ="lnkcity" runat ="server" Text =''
          <%#Eval("city") %>'>
        </asp:Label>
      </
ItemTemplate>
    </
asp:TemplateField>
  </
Columns>
</
asp:GridView>

Below is the code for getting the selected Row in Gridview using RowCommand Event.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "selectproduct")
    {
        textBox1.Text = Convert.ToString(e.CommandArgument.ToString());
    }

    if (e.CommandName == "selectvendor")
    {
        string vendid = Convert.ToString(e.CommandArgument.ToString());
        Response.Write("Vendorid:" + vendid);
    }
}


Below is the Image of GridView Seleted Row using RowCommand Event.

RowCommand2.gif

Thanks for reading my article.

Syed Shakeer Hussain.
 

Syed Shakeer

Shakeer is Working as a Software Engineer.He is expertise in WPF,SqlServer 2005/2008,Windows Moblie,C# and actively participating in R&D on Windows Mobile Development.

  • 240rank
  • 722K readers
COMMENT USING

Trending up