Add Event Handler To Dynamic Dropdownlist

Posted by Andrea Girardi on Stack Overflow See other posts from Stack Overflow or by Andrea Girardi
Published on 2011-01-17T22:40:24Z Indexed on 2011/01/17 22:53 UTC
Read the original article Hit count: 364

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about vb.net