asp.net building controls:
[quoted text, click to view] On Wed, 20 Jul 2005 14:26:24 -0700, Monty wrote:
>
> 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
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
Maybe I wasn't clear enough in my description. After the page loads
with the options, select "Record Four" on the page. After selecting,
the postback occurs and the controls are built again. One would think
that when the page rendered it would be reset back to "Number Two", but
it isn't.