Gridview rowcommand on button click

(ASP.NET)


Hi,

In this article i used rowcommand event in gridview on button click event to get the current record id.

.aspx page

 <div>
      <asp:GridView DataSourceID="sqlds_prodtype" AutoGenerateColumns="False"  
            OnRowCommand="grdvw_products_RowCommand"  AllowPaging="True" 
            AllowSorting="True" ID="grdvw_products" runat="server">

           <Columns>
               <asp:BoundField DataField="ProductID" HeaderText="Message ID"
        ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"
        ItemStyle-Width="170px" >
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>

<ItemStyle HorizontalAlign="Center" Width="170px"></ItemStyle>
    </asp:BoundField>
               <asp:BoundField DataField="ProductName"  HeaderText="ProductName" />
               <asp:BoundField DataField="SupplierID"  HeaderText="SupplierID" />
               <asp:BoundField DataField="CategoryID"  HeaderText="CategoryID" />
               <asp:ButtonField HeaderText="Products Details" ButtonType="Button" Text="Products"  CommandName="Products" />
           </Columns>

        </asp:GridView>
         <asp:SqlDataSource ID="sqlds_prodtype" runat="server" ConnectionString="<%$ ConnectionStrings:Northwindconstring %>"
            SelectCommand="SELECT * FROM Products" />
    </div>
Code behind:-

  protected void grdvw_products_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "Products")
                {
                    int index = Convert.ToInt32(e.CommandArgument);
                    GridViewRow row = grdvw_products.Rows[index];
                    //string productid = row.Cells[0].Text;
                    Int32 productid = Int32.Parse(row.Cells[0].Text);
                }
            }
            catch (Exception)
            { }
        }



Image

Ratings


Average Rating: 0.00 by 0 users
Giri Prasad
6/28/2013
6/28/2013
Download PDF

Comments