How do you prevent postback data from being loaded into controls?

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
 
M

Monty

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top