M
Monty
I've got a form that dynamically adds a DropDownList control to a page.
After the page is posted back the the code behind rebuilds the control
and resets the selected item. Problem is that the postback data
overrides what the code did. I would assume that no matter what it
would always be set to "Record Two" in the example below. Help?
Sample Code:
Public Class _Default
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim oDropDownList As New DropDownList
Dim oList As ListItem
With oDropDownList
.AutoPostBack = True
.ID = "test"
.EnableViewState = False
oList = New ListItem
oList.Value = "1"
oList.Text = "Record One"
.Items.Add(oList)
oList = New ListItem
oList.Value = "2"
oList.Text = "Record Two"
oList.Selected = True
.Items.Add(oList)
oList = New ListItem
oList.Value = "3"
oList.Text = "Record Three"
.Items.Add(oList)
oList = New ListItem
oList.Value = "4"
oList.Text = "Record Four"
.Items.Add(oList)
End With
phTest.Controls.Add(oDropDownList)
End Sub
End Class
After the page is posted back the the code behind rebuilds the control
and resets the selected item. Problem is that the postback data
overrides what the code did. I would assume that no matter what it
would always be set to "Record Two" in the example below. Help?
Sample Code:
Public Class _Default
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim oDropDownList As New DropDownList
Dim oList As ListItem
With oDropDownList
.AutoPostBack = True
.ID = "test"
.EnableViewState = False
oList = New ListItem
oList.Value = "1"
oList.Text = "Record One"
.Items.Add(oList)
oList = New ListItem
oList.Value = "2"
oList.Text = "Record Two"
oList.Selected = True
.Items.Add(oList)
oList = New ListItem
oList.Value = "3"
oList.Text = "Record Three"
.Items.Add(oList)
oList = New ListItem
oList.Value = "4"
oList.Text = "Record Four"
.Items.Add(oList)
End With
phTest.Controls.Add(oDropDownList)
End Sub
End Class