Events of DataGrid child controls

S

Scott M.

How do you access the event handlers for controls in Template columns of a
DataGrid?
 
J

Jos

Scott said:
How do you access the event handlers for controls in Template columns
of a DataGrid?

Right click on the datagrid.
Choose Edit Templates.
Select the column and the template from the dropdown list (in Web Matrix) or
select the column from the menu (in Visual Studio).
Select the control.
Modify its event handlers through the property panel.
 
S

Scott M.

Huh? There is no property of a control in a template column of a DataGrid
that has anything to do with an eventhandler. This is Web Form DataGrid.
 
J

Jos

Scott said:
Huh? There is no property of a control in a template column of a
DataGrid that has anything to do with an eventhandler. This is Web
Form DataGrid.

Yes, I had guessed that, and yes, you can set properties of controls
in template columns.

What is your editor?

The instructions I gave are for Web Matrix and Visual Studio.
 
S

Scott M.

I'm not asking how to set properties. I'm asking how to access the event
handler of a control in a template column of a DataGrid.
 
S

Steven Cheng[MSFT]

Hi Scott,

Thanks for posting. Not sure what you'd like to do on the sub control's
eventhandler. But if you want to referenct it, we need to get the sub
controls' control reference first. To do this, we can loop through the
DataGrid.Items collection and use FindControl on each DataGridItem's
certain Cell. For example:

private void Button1_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem item in this.dgMain.Items )
{
TextBox txt = item.Cells[0].FindControl("txtSub") as TextBox;
//txt.TextChanged ....
}
}

However, as I mentioned above, what do you want to do when get the
reference of the control? If you want programly attach event handlers onto
the controls, I think we'd better use the DataGrid's buildin "ItemCreated"
or "ItemDataBound" event to do it, otherwise, there may occur some
unexpected problems.
Please feel free to let me know if you have any further questions or
requriement. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Scott M.

Hi Steven,

I want to add a checkbox to each row of a DataGrid in a Template column. and
have users use these checkboxes as an alternate way to select a row in the
grid (this way they can select multiple items in the grid).

Of course, I need to be able to handle the CheckChanged event for each
checkbox. I am familiar with FindControl, but what do I do once I've gotten
a reference to the control.

I'm using VB.NET, not C#.

Thanks.
 
S

Steven Cheng[MSFT]

Hi Scott,

Thanks for your response. I've posted my reply and with a new VBNET version
demo page in your another thread in the group. I'd appreciate if you have a
look there. Also, if you feel convenient that we continue to followup in
that thread, please feel free to post there. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top