By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,677 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,677 IT Pros & Developers. It's quick & easy.

read rowindex from gridview (template column)

P: n/a
Hi,

I have a gridview with a template column. In the template column i have two
commandbuttons. When clicking the buttons I enter the cmd_click event - but
how do I read in which row the button was clicked??

I hope this makes sense?

/Claus
Oct 25 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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/lib...trols.gridview.
rowcommand.aspx

Please let me know if this answered your question or not. Thank you.

Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Oct 26 '06 #2

P: n/a
Hi Walter,

it seems to work - thanks a lot :-)

best regards
Claus

"Walter Wang [MSFT]" wrote:
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/lib...trols.gridview.
rowcommand.aspx

Please let me know if this answered your question or not. Thank you.

Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Oct 27 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.