CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

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

 



Comments

Peter van Ooijen said:

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 ?
# June 17, 2004 2:17 AM

cvacar said:

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
# June 17, 2004 10:21 AM

Colin said:

I have followed this code exactly - but still the event handlers fail to fire - what am i doing wrong ?
# February 3, 2005 3:54 PM

Peter van Ooijen said:

At what moment in the page life cycle are you executing it ?
That can be the diffrence between a fire and a non fire.
# February 3, 2005 5:46 PM

Rajesh Patel said:

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

# September 18, 2008 8:21 AM

pvanooijen said:

Didi you check AUTOEVENTWIREUP in the aspx ?

# September 22, 2008 4:29 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

This Blog

Syndication

News