473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

read rowindex from gridview (template column)

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
2 48388
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bishop | last post by:
I have a template column with an image in it. I assigned a skin to the image to adjust the width. I don't get an errors but it also dosn't change the width. Is it even possible to do what I'm...
2
by: rodchar | last post by:
hey all, you know in the gridView quick task menu how you have the option of taking a bound column and converting it into a TemplateColumn. Alright, once i do that i goto edit the template column...
0
by: GMartin | last post by:
I have a pop-up form with a three columned Grid that has checkboxes in a Template Column in the first/left-most column. (The form is to allow users to select "Members" of a group, where they check...
1
by: Neven Klofutar | last post by:
Hi, I have a questin regarding GridView, Template column and Checkbox in it. I'm working on a project that has several "client" applications entering data into DB, and one "admin" application...
0
by: jsreddy | last post by:
Hi All, I am having 4 text boxes in a Gridview Template Column. The Tab Sequence is set to 0 - 4. the tab sequence works fine when i have only one Row. if i am adding one more row to grid then tab...
0
by: sgandikota | last post by:
I'm using a a GridView TemplateColumn(linkbutton). When we double click on the Linkbutton in the templatefield, an event will be generated in the code behind file. How can we access the...
0
by: nityaprashant | last post by:
Hello.. could u plz help me? i have template column inside grid view.. in that one link button called Remove this? when i click on that link button confirmation box like "Are u sure u want...
3
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a generic list of data list <typelistcategory; listcategory = new list <type>; I also have a dropdown box that I have put into a template column of a gridview. I was able to populate...
1
by: rogerford | last post by:
I have a grid which i bind with values from Database. I have events to edit and update the grid. I am not using SqlDatasource to connect to DB. Rather i am doing the updating of the grid...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.