I'm trying to add an item to a DropDownList in a Setter, and the added item doesn't stay.
I have confirmed that Viewstate is correctly enabled as suggested in this question (http://stackoverflow.com/questions/445222/switched-to-asp-net-3-5-dropdownlist-doesnt-remember-dynamically-added-items)
Here is my code.
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