Add Event Handler To Dynamic Dropdownlist
- by Andrea Girardi
Hi all!
I have a page that contains dynamically generated Dropdown List controls and I want thant the dynamic dropdown list perform an AutoPostback to fill some other field using the value selected. This is the code I'm using to create dynamically the control:
If (Not IsPostBack) Then
Dim newDDL As DropDownList = New DropDownList()
AddHandler newDDL.SelectedIndexChanged, AddressOf ChangeValue
newDDL.ID = "Level1"
[fill the DropDownList]
newDDL.Items.Insert(results.Count, New ListItem("", -1))
newDDL.Width = "300"
newDDL.AutoPostBack = True
newDDL.SelectedIndex = results.Count
LevelDDLs.Controls.Add(newDDL)
LevelDDLs.Controls.Add(New LiteralControl("<br /><br />"))
End If
Control is correctly filled and rendered on ASP page but, after selecting a value, the page is reloaded (AutoPostBack is called) but the control is not diplayed and the sub is not called. I put a breakpoint into the ChangeValue sub but anything happens.
I read on some post that handler for the first DropDownList is not necessary but, how is it possible to tell DropDownList to call my sub after changevalue?
Could you help me, please?
many thanks,
Andrea