Peter's Gekko

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Adding eventhandlers at run time

Having published a dnj article on events in asp.net I got presented with the following problem :

The webform has an emty label and an empty table. At runtime the table is populated with a collection of imagebuttons. Every imagebutton will get an eventhandler.

private void buildTable(int rows, int cols)
{
   for (int
i =0; i < rows; i++)
   {
         TableRow tr = new
TableRow();
         for (int
j = 0; j < cols; j++)
         {
            TableCell tc = new
TableCell();
            ImageButton ib = new
ImageButton();
            ib.ToolTip = string.Format("This is button {0} {1}"
, i, j);
            ib.Click+=new
ImageClickEventHandler(ib_Click);
            tc.Controls.Add(ib);
            tr.Cells.Add(tc);
         }
      Table1.Rows.Add(tr);
   }
}

The method is quite straigthforward. It creates table rows, table cells and adds an imagebutton to every cell. The same eventhandler is used for all buttons

private void ib_Click(object sender, ImageClickEventArgs e)
{
   ImageButton ib = sender as ImageButton;
   Label1.Text = string.Format("ToolTip: {0} X : {1} Y : {2}", ib.ToolTip, e.X, e.Y);
}

Running the code the label will display which button was clicked. For the details you are invited to the original article.

The important part is the moment in the page life cycle the eventhandler is assigned. If the buildTable method is called from the page_load the event handler will work. If it is called in the pre-render the table will be populated but the eventhandlers will not fire. At that moment in the page life cycle the wire-up of events is allready set and fixed.

Peter

 


Posted 06-16-2004 9:17 AM by pvanooijen

[Advertisement]

Comments

cvacar wrote re: Adding eventhandlers at run time
on 06-16-2004 10:55 AM
Hello Peter,
I guess one way to solve this problem would be to do the following:
* since user input is needed in order to retrieve from the database the data based on which the table gets populated we cannot call the buildTable method in the PageLoad.
* we can collect the user input in one page then redirect to another page and in the second page's PageLoad we can call the buildTable method.

Can't see any other way.
Peter van Ooijen wrote re: Adding eventhandlers at run time
on 06-17-2004 2:17 AM
hello,
It's hard to say without knowing the app. But are you sure ?
The page load is hit on every roundtrip. On the first (couple of) roundtrips the user enters some input. On the next roundtrips the table can be built. The users input is read from texboxes and the like ?
cvacar wrote re: Adding eventhandlers at run time
on 06-17-2004 10:21 AM
I made a second page as I mentioned above. If the eventhandler is assigned when the page is FIRST loaded it all works fine: the event handlers fire as they should. Otherwise they don't. It's kind of ugly to have to have two pages for this, but can't see any other way. It sure beats dragging 96 ImageButton controls onto the page at design time...

Thanks,
Carmen
Colin wrote re: Adding eventhandlers at run time
on 02-03-2005 3:54 PM
I have followed this code exactly - but still the event handlers fail to fire - what am i doing wrong ?
Peter van Ooijen wrote re: Adding eventhandlers at run time
on 02-03-2005 5:46 PM
At what moment in the page life cycle are you executing it ?
That can be the diffrence between a fire and a non fire.
Rajesh Patel wrote re: Adding eventhandlers at run time
on 09-18-2008 8:21 AM

I have followed this code exactly - but still the event handlers fail to fire - what am i doing wrong ?

pvanooijen wrote re: Adding eventhandlers at run time
on 09-22-2008 4:29 PM

Didi you check AUTOEVENTWIREUP in the aspx ?

Stevo wrote re: Adding eventhandlers at run time
on 12-03-2008 2:07 PM

No, this code is crap.  The eventhandler doesn't fire!

pvanooijen wrote re: Adding eventhandlers at run time
on 12-04-2008 3:47 AM

I see many disappointed comments about the code not working. To sum up the points to take care off:

- Autoeventwireup

- The moment in the page lifecycle you're adding them, the page load happens before the handling of any clicks and the like, the prerender after that

- The attached event handlers only work in the current cycle, they are not preserved over roundtrips

Try.. and you will succeed.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?