Hi Claus,
Based on my understanding, you are using code such as:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ProductID"
HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName"
HeaderText="ProductName" SortExpression="ProductName" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="button1" runat="server"
OnClick="Button2_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and you want to know how to know which row the button belongs in the event
Button2_Click. Please correct me if I've misunderstood anything.
I think you can use following code to get the GridViewRow that the button
belongs:
protected void Button2_Click(object sender, EventArgs e)
{
GridViewRow row = ((Button)sender).Parent.Parent as GridViewRow;
Response.Write(row.RowIndex);
}
Normally, we don't handle the button's Click event in this way. A control
such as LinkButton or Button can bubble its click event to its parent
hierarchy until one handles it. To do this, we can define the button as
follows:
<asp:GridView ID="GridView2" runat="server"
AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="SqlDataSource1"
OnRowCommand="GridView2_RowCommand">
<Columns>
<asp:BoundField DataField="ProductID"
HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName"
HeaderText="ProductName" SortExpression="ProductName" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="button2" runat="server"
CommandName="MyCommand1" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can define the button's CommandName or CommandArgument property, for
example, you can bind CommandArgument to one of the binding field:
CommandArgument='<%# Eval("id") %>'
or set to the row index:
CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'
You can add multiple buttons to different Template and all will be handled
by GridView's RowCommand event:
protected void GridView2_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "MyCommand1") {
int rowIndex = int.Parse(e.CommandArgument);
}
}
You can differentiate which button gets clicked by checking e.CommandName.
Actually this CommandName is also used by other built-in command such as
Delete, Edit, Select, etc.:
#GridView.RowCommand Event (System.Web.UI.WebControls)
http://msdn2.microsoft.com/en-US/library/system.web.ui.webcontrols.gridview.
rowcommand.aspx
Please let me know if this answered your question or not. Thank you.
Sincerely,
Walter Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.