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.

<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



In .aspx source code page

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

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

      </ItemTemplate>
    </
asp:TemplateField>

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

    <asp:TemplateFieldHeaderText ="City">
      <ItemTemplate >
        <
asp:LabelID ="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.



Thanks for reading my article.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值