开发者

Add Event Handler To Dynamic Dropdownlist

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


You have to set the Dropdowns' IDs, otherwise they wouldn't recreate their selectedindex from ViewState and no events are raised. They have to get the same unique IDs on every postback.

Every control that is added dynamically also must be recreated on every postbacks. That should be done in Page_Load at the latest because after this page life cycle stage the viewstate is saved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜