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

Discussion in 'ASP .Net Building Controls' started by Monty, Jul 20, 2005.

  1. Monty

    Monty Guest

    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
     
    Monty, Jul 20, 2005
    #1
    1. Advertising

  2. Monty

    Monty Guest

    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.
     
    Monty, Jul 21, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Um9i?=
    Replies:
    3
    Views:
    14,553
    Steven Cheng[MSFT]
    Jan 21, 2004
  2. Itai
    Replies:
    0
    Views:
    552
  3. Stephen Adam
    Replies:
    1
    Views:
    940
    Scott Allen
    Nov 14, 2004
  4. Replies:
    2
    Views:
    927
  5. Itai
    Replies:
    0
    Views:
    174
Loading...

Share This Page