Dynamically created DropDownList loses ListItems on postback

I have a page containing some dynamically created controls (TextBox and DropDownList). When postback occurs, text fields retain their values, but DropDownLists lose their lists. This is pretty confusing as the DropDownList level on the page also maintains its list. Can anyone see what is wrong with the code below?

Any help in solving this problem would be greatly appreciated.

<%@ Page Language="VB"%> <script runat="server"> Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) If Not Page.IsPostBack Then ddlFixed.Items.Add(New ListItem("12", "13")) ddlFixed.Items.Add(New ListItem("14", "15")) End If Dim i As Integer For i = 0 To 3 Dim ddl As New DropDownList ddl.ID = "ddlPage" & i ddl.EnableViewState = True If Not Page.IsPostBack Then ddl.Items.Add(New ListItem("12", "13")) ddl.Items.Add(New ListItem("14", "15")) End If pnlDynamic.Controls.Add(ddl) Dim txtBx As New TextBox txtBx.ID = "txtPage" & i If Not Page.IsPostBack Then txtBx.Text = "Preset" End If pnlDynamic.Controls.Add(txtBx) Next End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server" enableviewstate="true"> <div> <br /> <br /> <a href="Default.aspx">Get-Request</a> <br /> <br /> <asp:Panel runat="server" ID="pnlDynamic" EnableViewState="true" /> <br /> <br /> <asp:DropDownList runat="server" ID="ddlFixed" /> <asp:Button runat="server" ID="btn" Text="Postback"/> </div> </form> </body> </html> 
+7
drop-down-menu viewstate
source share
5 answers

I found a solution that will allow me to maintain the viewstate via postback. It calls TrackViewState ItemCollection.

 CType(ddl.Items, IStateManager).TrackViewState() 

Thank you all for your help.

This is a working solution:

 <%@ Page Language="VB"%> <script runat="server"> Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) If Not Page.IsPostBack Then ddlFixed.Items.Add(New ListItem("12", "13")) ddlFixed.Items.Add(New ListItem("14", "15")) End If Dim i As Integer For i = 0 To 3 Dim ddl As New DropDownList ddl.ID = "ddlPage" & i CType(ddl.Items, IStateManager).TrackViewState() If Not Page.IsPostBack Then ddl.Items.Add(New ListItem("12", "13")) ddl.Items.Add(New ListItem("14", "15")) End If pnlDynamic.Controls.Add(ddl) Dim txtBx As New TextBox txtBx.ID = "txtPage" & i If Not Page.IsPostBack Then txtBx.Text = "Preset" End If pnlDynamic.Controls.Add(txtBx) Next End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server" > <div> <br /> <br /> <a href="Default.aspx">Get-Request</a> <br /> <br /> <asp:Panel runat="server" ID="pnlDynamic" /> <br /> <br /> <asp:DropDownList runat="server" ID="ddlFixed" /> <asp:Button runat="server" ID="btn" Text="Postback"/> </div> </form> </body> </html> 
+6
source share

Perhaps you need to explicitly activate ViewState for these DropDownLists ...

EDIT: This is what I mean by my last comment ...

  <script runat="server"> Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) If Not Page.IsPostBack Then ddlFixed.Items.Add(New ListItem("12", "13")) ddlFixed.Items.Add(New ListItem("14", "15")) Dim i As Integer For i = 0 To 3 Dim ddl As New DropDownList ddl.ID = "ddlPage" & i ddl.EnableViewState = True ddl.Items.Add(New ListItem("12", "13")) ddl.Items.Add(New ListItem("14", "15")) pnlDynamic.Controls.Add(ddl) Dim txtBx As New TextBox txtBx.ID = "txtPage" & i txtBx.Text = "Preset" pnlDynamic.Controls.Add(txtBx) Next End If End Sub </script> 
0
source share

DDL will save its elements and select when viewstate is enabled. The text field will maintain its contents regardless of the viewstate, since the resulting html will be returned to the server after the postback.

It seems to me that you have all these statements about representation in code and markup.

One of several things happens:

  • ViewState in the control is disabled.
  • ViewState over contrl hierachy is disabled (page, form, panel)
  • You dynamically add the control too late in the page loop to select which will be loaded from the viewstate. Uploading to INIT should be good.
0
source share

List items in DropDownList are not saved in view state. You will need to add them at each postback.

What is stored in the view state is the selected index - i.e. "value" of the control.

Edit: Well, it looks like I learned something today. I stand fixed :-)

-one
source share

I always use Page_Load and never experience such problems. Perhaps you could take a look.

I usually bind data to controls the first time the page loads, after which they allow Viewstate to handle callbacks, etc. Note that they suggest you bind data during Page_Load ...

Check out http://support.microsoft.com/kb/305141

Page events loadTOCNode (2, 'moreinformation'); During the life cycle of an ASP.NET page, several standard events that are exposed from the Page object are often used. The ASP.NET page structure automatically connects (or paves) the appropriate delegate instances when starting time for these methods. This saves you from writing the necessary β€œglue” of code. ”The following list shows the delegate instances that are connected to the startup time in the order in which they are launched:
  • Page_Init : during this event, you can initialize the values ​​or hook up any event handlers that you may have.
  • Page_Load : during this event, you can perform a series of actions to create an ASP.NET page for the first time or respond to client-side events that arise from a message. The state of the page and control was restored before this event. Use the IsPostBack page property to check if this page is being processed. If this is the first time, bind the data. Also, read and update management properties.
  • Page_DataBind : The DataBind event occurs when the DataBind method is called on the page level. If you call a DataBind on individual controls, it fires the DataBind event of the controls below it.
  • Page_PreRender : The PreRender event is fired just before the view state is saved and the controls are displayed. You can use this event to perform last-minute operations on your controls.
  • Page_Unload : after page processing is completed, the Page_Unload event occurs . This event is a good place to carry out the final cleaning job. This includes items such as clearing open database connections, dropping objects, or closing open files.
The following list contains events that are non-deterministic:
  • Page_Error : if an unhandled exception occurs during page processing, the Error event occurs . The Error event gives you the ability to gracefully handle errors.
  • Page_AbortTransaction : Transaction events are useful if you want to indicate whether a transaction is successful or unsuccessful. This event is typically used for shopping cart scenarios in which this event may indicate success or failure of the order. This event is if the transaction was aborted.
  • Page_CommitTransaction : This event fires when the transaction was completed successfully.
-one
source share

All Articles