Problem with DropDownLists and Post Backs

Discussion in 'ASP .Net' started by ~john, Dec 18, 2006.

  1. ~john

    ~john Guest

    I'm adding a DropDownList to my page in the code behind which is set to
    AutoPostBack... The problem is it's giving me the same SelectedValue on
    every post back, the value of the first item in the list... The
    DropDownList itself retains the correct selected value (the option I
    select in the list is the option the dropdown list retains on postback
    -but- the property "SelectedValue" is always the first item in the list
    for some reason and not the value that I selected from the
    DropdownList. any ideas?

    protected void Page_Load(object sender, EventArgs e)
    {


    DropDownList DropDownList_EquipmentTypes = new DropDownList();
    DropDownList_EquipmentTypes.ID = "myList";
    DropDownList_EquipmentTypes.DataSource =
    ObjectDataSource_EquipmentTypes;
    DropDownList_EquipmentTypes.DataTextField =
    "EquipmentTypeDesc";
    DropDownList_EquipmentTypes.DataValueField = "EquipmentTypeID";
    DropDownList_EquipmentTypes.DataBind();
    DropDownList_EquipmentTypes.AutoPostBack = true;


    PlaceHolder1.Controls.Add(DropDownList_EquipmentTypes);

    //This value is always the first item in the list and not the
    value selected????
    string x = DropDownList_EquipmentTypes.SelectedValue;

    }
    ~john, Dec 18, 2006
    #1
    1. Advertising

  2. ~john

    TiSch Guest

    On 18 Dez., 22:49, "~john" <> wrote:
    > I'm adding a DropDownList to my page in the code behind which is set to
    > AutoPostBack... The problem is it's giving me the same SelectedValue on
    > every post back, the value of the first item in the list... The
    > DropDownList itself retains the correct selected value (the option I
    > select in the list is the option the dropdown list retains on postback
    > -but- the property "SelectedValue" is always the first item in the list
    > for some reason and not the value that I selected from the
    > DropdownList. any ideas?
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    >
    > DropDownList DropDownList_EquipmentTypes = new DropDownList();
    > DropDownList_EquipmentTypes.ID = "myList";
    > DropDownList_EquipmentTypes.DataSource =
    > ObjectDataSource_EquipmentTypes;
    > DropDownList_EquipmentTypes.DataTextField =
    > "EquipmentTypeDesc";
    > DropDownList_EquipmentTypes.DataValueField = "EquipmentTypeID";
    > DropDownList_EquipmentTypes.DataBind();
    > DropDownList_EquipmentTypes.AutoPostBack = true;
    >
    > PlaceHolder1.Controls.Add(DropDownList_EquipmentTypes);
    >
    > //This value is always the first item in the list and not the
    > value selected????
    > string x = DropDownList_EquipmentTypes.SelectedValue;
    >
    > }


    Hello,
    you have to bind the dropdownlist to the datasource only on the first
    time(If not Page.IsPostback).

    Regards,
    Tim
    TiSch, Dec 18, 2006
    #2
    1. Advertising

  3. ~john

    ~john Guest

    TiSch wrote:

    > Hello,
    > you have to bind the dropdownlist to the datasource only on the first
    > time(If not Page.IsPostback).
    >
    > Regards,
    > Tim


    I tried adding the bind() inside a if(!page.IsPostback) and what that
    does is loads the dropdownlist initially but doesn't load any data into
    after a post back... I'm guessing because the page IS a post-back so
    bind() never gets called.
    ~john, Dec 18, 2006
    #3
  4. ~john

    bruce barker Guest

    because you are creating you dropdown dynamically, you should move the
    code to oninit. creating in pageload is too late to get postback values.

    -- bruce (sqlwork.com)

    ~john wrote:
    > I'm adding a DropDownList to my page in the code behind which is set to
    > AutoPostBack... The problem is it's giving me the same SelectedValue on
    > every post back, the value of the first item in the list... The
    > DropDownList itself retains the correct selected value (the option I
    > select in the list is the option the dropdown list retains on postback
    > -but- the property "SelectedValue" is always the first item in the list
    > for some reason and not the value that I selected from the
    > DropdownList. any ideas?
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    >
    >
    > DropDownList DropDownList_EquipmentTypes = new DropDownList();
    > DropDownList_EquipmentTypes.ID = "myList";
    > DropDownList_EquipmentTypes.DataSource =
    > ObjectDataSource_EquipmentTypes;
    > DropDownList_EquipmentTypes.DataTextField =
    > "EquipmentTypeDesc";
    > DropDownList_EquipmentTypes.DataValueField = "EquipmentTypeID";
    > DropDownList_EquipmentTypes.DataBind();
    > DropDownList_EquipmentTypes.AutoPostBack = true;
    >
    >
    > PlaceHolder1.Controls.Add(DropDownList_EquipmentTypes);
    >
    > //This value is always the first item in the list and not the
    > value selected????
    > string x = DropDownList_EquipmentTypes.SelectedValue;
    >
    > }
    >
    bruce barker, Dec 19, 2006
    #4
  5. ~john

    ~john Guest

    bruce barker wrote:
    > because you are creating you dropdown dynamically, you should move the
    > code to oninit. creating in pageload is too late to get postback values.
    >
    > -- bruce (sqlwork.com)
    >



    Thanks... could you give a short example of this? That would be greatly
    appreciated.

    ~john
    ~john, Dec 19, 2006
    #5
  6. ~john

    Mark Rae Guest

    "~john" <> wrote in message
    news:...

    > Thanks... could you give a short example of this?


    protected void Page_Init(object sender, EventArgs e)
    {
    DropDownList DropDownList_EquipmentTypes = new DropDownList();
    // etc
    }
    Mark Rae, Dec 19, 2006
    #6
    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. Mark S.

    Post Backs

    Mark S., Aug 13, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    784
    George Ter-Saakov
    Aug 13, 2003
  2. ALPO

    Stopping post backs possible??

    ALPO, Nov 21, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    324
    Joao S Cardoso [MVP]
    Nov 21, 2003
  3. Jason
    Replies:
    5
    Views:
    370
    darrel
    Dec 10, 2004
  4. Rosanne
    Replies:
    0
    Views:
    480
    Rosanne
    Jun 17, 2005
  5. Dirc Khan-Evans
    Replies:
    6
    Views:
    595
    Rosanne
    Oct 20, 2005
Loading...

Share This Page