Register
|
Articles
|
Questions
|
Projects
|
Asp.Net Tips
|
Free Magazines
|
Tutorials
|
Search
|
Write to us
|
Login
Search
Articles
Asp.Net 2.0
AJAX
ADO.Net
C#
Javascript
Books
Asp.Net 2.0
AJAX
ADO.NET
C#
JavaScript
MS SQL Server
SilverLight
VB.NET
XML
Web Service
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
How To get RowIndex of Asp.Net GridView in the RowCommand Event?
Posted by
Moderator1
on
4/3/2009 1:04:30 AM
15393
Views
It is not possible to get the RowIndex of the GridView row, in the RowCommand Event, when you trigger a control that is placed inside a
TemplateField section. But there are two methods to get the RowIndex in the RowCommand Event. Let us 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();
}
}
Post New Tips/Tricks
View all Tips/Tricks
Featured Resources:
Simply JavaScript - Free 150 Page Preview!
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
The JavaScript Anthology: 101 Essential Tips, Tricks & Hacks - Free 158 Page Preview
Get the most out of this complete question-and-answer book on JavaScript.
100% Free Subscription until Stock lost.
View complete list.
Sponsored by
Home
About us
Contact Us
Links
Advertise
Privacy Policy
Copyright ©
2012
. www.AspdotnetCodes.com. All rights reserved.