Grid

N

Nice Chap

I have a datagrid.
I am using a template with two fields : A label and a button.
At run time the datagrid contains around 10 rows.
All ten rows have a label and a button each, but ...

How do I figure out which button was clicked ? (which row )

I have noticed that VS does not create variables for the controls in the
template.

Please help.
 
S

Siva M

Assuming you have the template column like this:
....
<ItemTemplate>
<asp:Button runat=server id=btnX Text="Click Me" onClick="ServerClick"
/>
</ItemTemplate>
....

In the code behind, have:

private void ServerClick (object sender, EventArgs e)
{
Button target = (Button)sender; // Button that was clicked
DataGridItem dgi = (DataGridItem)(target.Parent.Parent); // Datagrid row
of that button
}

Note that you may have to traverse up more 'Parent's based on the level of
the button's nesting in the control hierarchy. Alternatively, you can loop
through all the grid rows and find the button:

In the ServerClick event:
....
DataGridItem x = null;
foreach (DataGridItem dgi in MyDataGrid.Items)
{
if (dgi.FindControl (target.ID) != null)
{
x = dgi;
break;
}
}
// x now references the clicked button's Datagrid row.

Hope this helps.

I have a datagrid.
I am using a template with two fields : A label and a button.
At run time the datagrid contains around 10 rows.
All ten rows have a label and a button each, but ...

How do I figure out which button was clicked ? (which row )

I have noticed that VS does not create variables for the controls in the
template.

Please help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top