Can't repopulate DropdownList control during postback?

B

Bill Cohagan

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
 
B

Bill Cohagan

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
 
S

Steven Cheng[MSFT]

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.
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top