Click here to Skip to main content
11,706,639 members (31,343 online)
Click here to Skip to main content

Handling events of child controls inside a DataGrid

, 2 Jun 2004 208.9K 1.5K 46
Rate this:
Please Sign up or sign in to vote.
How to hook up on an event of a web control residing inside a DataGrid cell.

Introduction

One of the big DataGrid mysteries is (or was, at least for me) the handling of events triggered by controls from within a DataGrid cell and not dealt with by the DataGrid. An example is handling of the OnCheckedChanged event of a CheckBox Control with enabled AutoPostBack. On the other hand than these non preset postback controls are button controls with default postback and support of a command attribute for which the DataGrid provides sufficient assistance.

Background

If you, like me, found that using a DataGrid is one of the most convenient ways to handle any data-driven web applications, you might quite often have experienced its limitations. If you need to get things done quickly, you might not want to write an entire column control yourself, but just use a TemplateColumn. Within this TemplateColumn, you would probably want to use some web controls and of course take advantage of all their native features, including events. But this doesn't work out quite as smoothly as it might sound. In the course of experimenting with controls inside a DataGrid, I experienced quite a lot of odd runtime compiler errors till I ran across one of the most interesting and useful DataGrid events: the ItemCreated event.

How-To

What we want to do here is just hook a handler routine onto the OnCheckedChanged event of the CheckBox Control. The TemplateColumn with the CheckBox might look like the following example:

<asp:TemplateColumn HeaderText="Checkbox"> 
 <ItemTemplate> 
   <asp:CheckBox ID="cbExample" AutoPostBack="true" runat="server"> 
   </asp:CheckBox> 
 </ItemTemplate> 
</asp:TemplateColumn> 

The function for handling the event in the undelying code will look very similar to this:

protected void OnChangeHandler(object sender, System.EventArgs e)
{
 // Handle the event...
}

So where are we going to connect the handler and the control? If we put the registration directly inside the Control such as OnCheckChanged="OnChangeHandler" while using code to support it, we will probably end up with a nice runtime compiler error. By applying the idea of delegate registration from VisualStudio, we can just do it at runtime. Using the ItemCreated event of the DataGrid (see code below) works out best for me, especially because I usually do a lot of other tweaking around like merging footer cells and switching sorting symbols.

private void grid_ItemCreated( object sender,
 System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 ListItemType elemType = e.Item.ItemType;
 if ((elemType == ListItemType.Item)||(elemType == ListItemType.AlternatingItem))
 {
  // *** Event Handler for Checkbox *** 
  CheckBox cBox = (CheckBox) e.Item.FindControl("cbExample");
  cBox.CheckedChanged += new EventHandler(OnChangeHandler);
 }
}

Points of Interest

Handling DataGrid and related events is sometimes a cumbersome task, but the DataGrid is still one of my favorite controls. I would be happy to hear your solutions to this kind of problem. Particular points of interest: whether you simply placed a function call inside another DataGrid event, and why you preferred this solution in your particular situation.

History

  • First Post on 22 Apr 2004.
  • Added a VS.net 2003 sample project on 29 May 2004.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

mkruppa
Team Leader
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questioncan i have this code in vb.net Pin
malikamjad5-Jul-06 2:14
membermalikamjad5-Jul-06 2:14 
AnswerRe: can i have this code in vb.net Pin
Rupesh Mhatre18-Nov-07 21:15
memberRupesh Mhatre18-Nov-07 21:15 
GeneralAlternatively you could do this Pin
sachinsrb22-Dec-05 0:03
membersachinsrb22-Dec-05 0:03 
GeneralRe: Alternatively you could do this Pin
Mika24-Apr-06 23:48
memberMika24-Apr-06 23:48 
GeneralRe: Alternatively you could do this Pin
Member 1062122314-Jan-15 12:08
memberMember 1062122314-Jan-15 12:08 
GeneralObject reference not set to an instance of an object Pin
Anonymous12-Oct-05 4:48
sussAnonymous12-Oct-05 4:48 
GeneralThanks Pin
hsu_kf12-Jul-05 21:11
memberhsu_kf12-Jul-05 21:11 
QuestionMabye you know, what I'm doing wrong? Pin
thomasa24-Apr-05 22:43
memberthomasa24-Apr-05 22:43 
Generalnever found a child control when usign Edit Templete Pin
carzel3-Feb-05 9:46
membercarzel3-Feb-05 9:46 
QuestionHow about user controls? Pin
Joe Cleland20-Jan-05 6:21
memberJoe Cleland20-Jan-05 6:21 
GeneralUrgent please help. iTrying to implement it with a radiobuttonlist and vb.net Pin
luisvalencia5-Jan-05 9:24
memberluisvalencia5-Jan-05 9:24 
GeneralOnCheckChanged=&quot;OnChangeHandler&quot; - will work Pin
rabashani6-Nov-04 6:16
sussrabashani6-Nov-04 6:16 
GeneralRe: OnCheckChanged=&quot;OnChangeHandler&quot; - will work Pin
mkruppa9-Nov-04 1:34
membermkruppa9-Nov-04 1:34 
GeneralNew header Pin
Fernando Finelli23-Jul-04 5:12
sussFernando Finelli23-Jul-04 5:12 
GeneralHaving trouble working your code on the event handler Pin
ramjisk18-Jun-04 4:56
memberramjisk18-Jun-04 4:56 
GeneralRe: Having trouble working your code on the event handler Pin
mkruppa18-Jun-04 6:17
membermkruppa18-Jun-04 6:17 
GeneralRe: Having trouble working your code on the event handler Pin
ramjisk18-Jun-04 7:42
memberramjisk18-Jun-04 7:42 
GeneralRe: Having trouble working your code on the event handler Pin
mkruppa18-Jun-04 11:00
membermkruppa18-Jun-04 11:00 
GeneralRe: Having trouble working your code on the event handler Pin
ramjisk18-Jun-04 11:11
memberramjisk18-Jun-04 11:11 
GeneralRe: Having trouble working your code on the event handler Pin
ramjisk18-Jun-04 7:51
memberramjisk18-Jun-04 7:51 
GeneralRe: Having trouble working your code on the event handler Pin
mkruppa18-Jun-04 11:23
membermkruppa18-Jun-04 11:23 
GeneralRe: Having trouble working your code on the event handler Pin
ramjisk21-Jun-04 3:11
memberramjisk21-Jun-04 3:11 
GeneralRe: Having trouble working your code on the event handler Pin
mkruppa24-Jun-04 22:44
membermkruppa24-Jun-04 22:44 
Generalunruledboy Pin
Chen Huisheng3-Jun-04 2:31
memberChen Huisheng3-Jun-04 2:31 
GeneralRe: unruledboy Pin
mkruppa3-Jun-04 3:10
membermkruppa3-Jun-04 3:10 
GeneralRe: unruledboy Pin
ValeriyP4-Jun-07 7:22
memberValeriyP4-Jun-07 7:22 
QuestionNow to get the datagrids [e.Item.ItemIndex]? Pin
TheChad24-May-04 8:36
memberTheChad24-May-04 8:36 
AnswerRe: Now to get the datagrids [e.Item.ItemIndex]? Pin
mkruppa24-May-04 10:32
membermkruppa24-May-04 10:32 
GeneralDemo Pin
Salam Elias22-May-04 4:00
memberSalam Elias22-May-04 4:00 
GeneralRe: Demo Pin
mkruppa24-May-04 10:24
membermkruppa24-May-04 10:24 
GeneralRe: Demo Pin
Salam Elias25-May-04 9:54
memberSalam Elias25-May-04 9:54 
Generalhandle event sample Pin
S'KO13-May-04 18:28
sussS'KO13-May-04 18:28 
GeneralRe: handle event sample Pin
mkruppa14-May-04 6:24
membermkruppa14-May-04 6:24 
GeneralRe: handle event sample Pin
sukoco halim16-May-04 21:09
membersukoco halim16-May-04 21:09 
GeneralRe: handle event sample Pin
mkruppa16-May-04 22:16
membermkruppa16-May-04 22:16 
GeneralRe: handle event sample Pin
sukoco halim17-May-04 0:14
membersukoco halim17-May-04 0:14 
GeneralRe: handle event sample Pin
mkruppa17-May-04 23:57
membermkruppa17-May-04 23:57 
GeneralRe: handle event sample Pin
sukoco halim31-May-04 23:58
membersukoco halim31-May-04 23:58 
Generalquestion... Pin
l a u r e n29-Apr-04 9:50
memberl a u r e n29-Apr-04 9:50 
GeneralRe: question... Pin
mkruppa29-Apr-04 16:43
membermkruppa29-Apr-04 16:43 
GeneralThanks Pin
NehalSS28-Apr-04 18:55
memberNehalSS28-Apr-04 18:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 3 Jun 2004
Article Copyright 2004 by mkruppa
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid