Wednesday, August 31, 2016

Retrieve current row index of an ASP.NET GridView control on RowCommand event

Hi, To retrieve the current row index of a GridView control on RowCommand event, you have to access the NamingContainer property of a control which belongs to that GridViewRow. In the example below, I have a LinkButton control declared inside a TemplateField.
ASPX Code
<asp:TemplateField>
 <ItemTemplate>
  <asp:LinkButton ID="lbtnDelete" CSSClass="btn" CommandName="Delete" Text="Delete" runat="server"/>
 </ItemTemplate>
</asp:TemplateField>
Code Behind
protected void gvCustomers_RowCommand(object sender, GridViewCommandEventArgs e)
{
 if (e.CommandName == "Delete")
 {
  GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
  int RowIndex = gvr.RowIndex;
  //TODO: other codes here...
 }
}

0 comments:

Post a Comment