动态创建的DropDownList在Postback上丢失了ListItems

我有一个包含一些动态创建的控件(TextBox和DropDownList)的页面。 当回发发生时,TextBoxes保留它们的值,但DropDownLists失去它们的ListItems。 这很令人困惑,因为页面级别的DropDownList也会保留它的ListItems。 任何人都可以看到下面的代码有什么问题吗?

任何帮助解决这个问题将不胜感激。

<%@ 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>

我找到了可以让我在回传时保持视图状态的解决方案。 这是调用ItemCollection的TrackViewState。

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

感谢大家的帮助。

这是工作解决方案:

<%@ 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>

当viewstate被启用时,DDL将维护它的项目和选择。 文本框将保持其内容,无论视图状态如何,因为生成的html会在回发中写回服务器。

我发现你的代码和标记中包含了所有这些视图状态语句。

几件事情之一正在发生:

  • 控件上的ViewState被禁用
  • 控制层面上方的ViewState被禁用(页面,窗体,面板)
  • 您正在页面周期中动态地添加控件,以便从viewstate中加载选择。 在INIT中加载应该没问题。

  • 也许你需要为这些DropDownLists显式激活ViewState ...

    编辑:这是我的意思是我最后的评论...

        <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>
    
    链接地址: http://www.djcxy.com/p/42379.html

    上一篇: Dynamically created DropDownList loses ListItems on Postback

    下一篇: How do you organize your resource files?