Can't repopulate DropdownList control during postback?

Discussion in 'ASP .Net Web Controls' started by Bill Cohagan, Sep 29, 2006.

  1. Bill Cohagan

    Bill Cohagan Guest

    I've run into what appears to be a circular problem; at least it's got me
    going in circles. I've got a dropdown that I want to repopulate when a
    particular event occurs; i.e., during a postback for that event. What I need
    to do is set the DataSource to a new value, a List<Room> collection. I then
    need to set the dropdown's SelectedValue property to a new value.

    Here's the (simplified) code:

    roomList.DataSource = rooms; // of type List<Room>
    roomList.DataBind(); // Exception thrown here
    roomList.SelectedValue = room.RoomID.ToString(); // room is of type Room
    and IS in the collection, rooms.

    In the above case I get an exception when it happens that the (old)
    SelectedValue does not have a corresponding entry in the (new) rooms list.
    The "obvious fix" is:

    roomList.DataSource = rooms;
    roomList.SelectedValue = room.RoomID.ToString(); //Exception thrown here
    roomList.DataBind();

    In this case however I get the error when the (new) SelectedValue doesn't
    have an entry in the (old) DataSource list. I've tried various combinations
    of setting SelectedID = 0, calling the ClearSelection method, etc., but
    nothing seems to work. The docs say that it's not possible to have a
    DropDownList control with *no* current selection, so I didn't really expect
    these last attempts to help.

    So, what (IF ANY?!!) incantation might I use in order to reassign both the
    DataSource and SelectedValue properties of a DropDownList control during a
    postback?

    Thanks in advance,

    Bill
    Bill Cohagan, Sep 29, 2006
    #1
    1. Advertising

  2. Bill Cohagan

    Bill Cohagan Guest

    I think I figured it out. The magic appears to be in using

    roomList.Items.Clear()

    to return the roomList DropDownList control to a state where you can
    reassign DataSource and SelectedValue. At least for the moment ....

    Bill

    "Bill Cohagan" <> wrote in message
    news:%23$...
    > I've run into what appears to be a circular problem; at least it's got me
    > going in circles. I've got a dropdown that I want to repopulate when a
    > particular event occurs; i.e., during a postback for that event. What I
    > need to do is set the DataSource to a new value, a List<Room> collection.
    > I then need to set the dropdown's SelectedValue property to a new value.
    >
    > Here's the (simplified) code:
    >
    > roomList.DataSource = rooms; // of type List<Room>
    > roomList.DataBind(); // Exception thrown here
    > roomList.SelectedValue = room.RoomID.ToString(); // room is of type
    > Room and IS in the collection, rooms.
    >
    > In the above case I get an exception when it happens that the (old)
    > SelectedValue does not have a corresponding entry in the (new) rooms list.
    > The "obvious fix" is:
    >
    > roomList.DataSource = rooms;
    > roomList.SelectedValue = room.RoomID.ToString(); //Exception thrown here
    > roomList.DataBind();
    >
    > In this case however I get the error when the (new) SelectedValue doesn't
    > have an entry in the (old) DataSource list. I've tried various
    > combinations of setting SelectedID = 0, calling the ClearSelection method,
    > etc., but nothing seems to work. The docs say that it's not possible to
    > have a DropDownList control with *no* current selection, so I didn't
    > really expect these last attempts to help.
    >
    > So, what (IF ANY?!!) incantation might I use in order to reassign both the
    > DataSource and SelectedValue properties of a DropDownList control during a
    > postback?
    >
    > Thanks in advance,
    >
    > Bill
    >
    Bill Cohagan, Sep 29, 2006
    #2
    1. Advertising

  3. Hi Bill,

    The DropDownList.SelectedValue is inherited from the "ListControl" class.
    And the "set" accessor of it has the following code logic:

    ** if the new assigned value is null, it will clear the selection status of
    all the items in the listcontrol, that means no item is selected

    ** if the new assigned value(string) is not null, it will check the current
    list's Items collection

    ** if current Items collection is empty , it will cache the selected
    value and won't try to find the item which matchs the assigned value
    ** if the curent items collection is not empty, it will try finding the
    item which matchs the assigned value from the Items collection


    Thus, for your scenario, you can use the following means to avoid exception
    before you repopulate the list:

    1) clear the list first as you've found

    2) assign "Null" to the SelectedValue property to clear the list's select
    status.

    Hope this also helps.


    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Sep 29, 2006
    #3
    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. TaeHo Yoo
    Replies:
    4
    Views:
    729
  2. luna_s

    popup repopulate

    luna_s, Mar 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    332
    Teemu Keiski
    Mar 6, 2004
  3. JN

    Repopulate TreeView

    JN, Jan 26, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,126
  4. TS

    explanation of when need to repopulate control

    TS, Aug 2, 2005, in forum: ASP .Net Building Controls
    Replies:
    28
    Views:
    524
    Steven Cheng[MSFT]
    Aug 24, 2005
  5. TS

    explanation of when need to repopulate control

    TS, Aug 2, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    115
    Steven Cheng[MSFT]
    Aug 3, 2005
Loading...

Share This Page