Im having what I believe to be a similar issue with the dynamic dropdownlist control and the above advice didn't help. Heres the situation: If I create a runtime asp dropdownlist and populate it with an array list, I can choose the selected value, but only the first time the page loads.
My dropdownlist is created from a function called from page load every time like so:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
poplist()
End Sub
Function poplist()
Dim k As New DropDownList
k.EnableViewState = False
'array for costcategory choices
Dim myar(2, 2)
myar(0, 0) = "1"
myar(1, 0) = "value 1"
myar(0, 1) = "2"
myar(1, 1) = "value 2"
'populate all the listitems
For i As Integer = 0 To UBound(myar, 2)
Dim myl As New ListItem
myl.Value = myar(0, i)
myl.Text = myar(1, i)
k.Items.Add(myl)
Next
Dim lb As ListItem = k.Items.FindByValue("2")
If Not lb Is Nothing Then lb.Selected = True
PlaceHolder1.Controls.Add(k)
End Function
The problem is that the selected index is only forced when the page first loads. If I have this page load, then select 'value 1' and then hit a linkbutton or anything else that triggers a postback, I would figure that the control is built again completely from scratch and 'value2' would be selected. To my dismay, 'value 1' is remembered. How could this be? My debuging follows all the seemingly logical steps including selecting the selected dropdownlist index. After postback, it just dont take. Its like my code is superceeded by something remembered. Please help.