0
J'essaie d'ajouter un élément à une DropDownList dans un Setter, et l'élément ajouté ne reste pas.Impossible d'ajouter l'élément à DropDownList dans la propriété Setter dans ASP .NET
Je confirme que Viewstate est correctement activé comme suggéré dans cette question (Switched to ASP.NET 3.5, DropDownList doesn't remember dynamically added items)
Voici mon 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
Cela a fonctionné - je ne serais jamais tombé sur cette propriété. Merci beaucoup! –