代码之家  ›  专栏  ›  技术社区  ›  RB.

无法在ASP.NET的Setter属性中将项添加到DropDownList

  •  0
  • RB.  · 技术社区  · 15 年前

    我试图将一个项目添加到Setter中的DropDownList,但添加的项目不会保留。

    我已确认Viewstate已按此问题中的建议正确启用( Switched to ASP.NET 3.5, DropDownList doesn't remember dynamically added items )

    这是我的密码。

        Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            If ddlCountry.Items.Count < Country.GetList.Length Then
                ddlCountry.DataSource = Country.GetList()
                ddlCountry.DataBind()
                'At this point, there are correctly 231 items in ddlCountry.'
            End If
        End Sub
    
        Public WriteOnly Property Country() As String
            Set(ByVal Value As String)
                If ddlCountry.Items.FindByValue(Value.Country) Is Nothing Then
                    Dim li As New ListItem(Value.Country, Value.Country)
                    ddlCountry.Items.Insert(0, li)
                    ddlCountry.SelectedIndex = ddlCountry.Items.IndexOf(li)
                    'At this point, there are correctly 232 items in ddlCountry'
                Else
                    ddlCountry.SelectedValue = Value.Country
                End If
            End Set
        End Property
    
        Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
            If ddlCountry.Items.FindByText("<-- Please Select-->") Is Nothing Then
                 'At this point, we are incorrectly'
                 'back to 231 items - this is the problem.'
                 ddlCountry.Items.Insert(0, New ListItem("<-- Please Select-->", ""))
            End If
        End Sub
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Johan Leino    15 年前

    是否尝试在dropdownlist上将AppendDataBoundItems属性设置为true?