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
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