Obtain RowIndex of Asp.Net GridView in the RowCommand Event
There are two methods to get the RowIndex in the RowCommand Event. Let see one-by-one.
Consider you have a LinkButton placed inside the TemplateField section as follows.
<asp:TemplateField HeaderText=”Submit”>
<ItemTemplate>
<asp:LinkButton ID=”lnkbtnSubmit” runat=”server” CommandName=”Submit” Text=’<%# Bind(“Id”) %>’ ></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</asp:TemplateField>
Method 1: Using CommandSource object
In the RowCommand Event of the GridView control, write the following code,
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals(“Submit”))
{
GridViewRow gvr = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
int RowIndex = gvr.RowIndex;
}
}
Method 2: Using CommandArgument property
In the CommandArgument property of the LinkButton, add an inline code to assign the GridViewRow’s container RowIndex value as follows,
<asp:TemplateField HeaderText=”Submit”>
<ItemTemplate>
<asp:LinkButton ID=”lnkbtnSubmit” runat=”server” CommandName=”Submit” Text=’<%# Bind(“Id”) %>’
CommandArgument=’<%# ((GridViewRow) Container).RowIndex %>’ ></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</asp:TemplateField>
Then in the RowCommand Event, you can get the RowIndex as follows,
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals(“Submit”))
{
int RowIndex = Convert.ToInt32(e.CommandArgument).ToString();
}
}
Have a Look at ASP.Net Development Company
No comments yet.
Leave a Reply
-
Archives
- April 2009 (5)
-
Categories
-
RSS
Entries RSS
Comments RSS
