C# - Dynamically creating controls and adding event handlers

Soldato
Joined
12 Jan 2004
Posts
6,824
Location
Londinium
Hey guys,

I've come across an issue that I can't seem to resolve. I have a web form with an "add row" button, and a table control. There is also a NumRows property (whose value is stored in a session object) that defines the number of rows to build. When add row button is clicked it increments NumRows. There is also a BuildTable() function that is called from the OnInit() function. BuildTable adds rows to the table for the value of NumRows. Each row contains a textbox and a DeleteRow button (which is assigned an event handler).

Right, the problem I have is that events can only be assigned to controls in either the OnInit or OnLoad functions, but the postback events occur after the Load event. So if a user clicks on the add row button, the NumRows value only gets updated after the BuildTable function has been called, so the correct number of rows will not be added. If BuildTable is called after the AddRow click event, the correct number of rows get added but then the new button events will never be assigned (as you have to do it in Init or Load).

Does anyone know how I can get around this problem without resorting to using javascript?
 
Back
Top Bottom