Add events to array of controls created at runtime



Hi all,

I've got a web form that I've written code to create an array of
DropDownList controls on the page depending on how many records are pulled
back. The code to create the controls is working fine but now I need to add
events to the newly created DropDownList controls. I need to add the
SelectedIndexChanged event and I'm having a hard time getting the code to add
events to the controls since the names (IDs) are varied to include the record
row number like "name & (int)".

I've seen examples on other web-sites which have either syntax errors and/or
so do not compile on my pc. So my question is this...

If I have a group of dropdownlistboxes named dlstDropDown and I have changed
the ID of the controls before I add the control to my page to include an
index so they would be dlstDropDown0, dlstDropDown1, dlstDropDown2, etc...
How do I add a SelectedIndexChanged event to each of them? And/Or how would
I go about creating a generic private sub to capture this event?

What I've tried so far with no success is:

'before I add the control to the page
AddHandler dlstWindow.SelectedIndexChanged, AddressOf _

It looks like the event either doesn't fire or isn't in the correct
AddressOf. This is the sub I've been playing with:

Private Sub dlstWindow_SelectedIndexChanged(ByVal sender As Object, ByVal _
e As System.EventArgs)

Dim strCurrentID As String
Dim currentDropDownList As DropDownList = CType(sender, DropDownList)

strCurrentID = currentDropDownList.ID

End Sub

I'm using 2003 (haven't migrated to 2005 yet). Any help would be greatly
appreciated. I've already burned an entire day on this and am working on
burning another one. :(

thanks in advance,


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question