Dynamic Buttons and Event Handlers, revisited


Boban Dragojlovic

The Concept:

I dynamically create an HTML table (with many rows) each with its own
button. When the user clicks on the button, I wan't to know which row's
button they clicked.

The Details:

On the initial pass through a page (e.g. when NOT Page.isPostBack) I read a
SQL table, and for each row in the table I create a dynamic row in an HTML
table, including a button. I also assign an event handler for the new

b = New Button
b.Text = "Select"
b.ID = "btnSelect" & iCount
AddHandler b.Click, AddressOf SelectRow

I also record how many buttons I created in the ViewState.

When the page is posted back (within Page_Init), I recreate the buttons,
including the event handler, as follows:

For i = 1 To CInt(ViewState("nbrButtons"))
b = New Button
b.ID = "btnSelect" & i
AddHandler b.Click, AddressOf SelectRow
Next i

Yet in spite of this, I can never get the SelectRow event handler to fire.