Author: csi95 | Posted on: 11/11/2008 10:18:37 AM | Views : 301

Hi Folks.  I'm at a loss with this one, so hopfully someone here can help.
I've got a simple web form with a DropDownList that's populated from an SqlDataSource.  I also have a Button.  When I click the button, I want to be able to set the SelectedValue of the DropDownList.  Sounds simple, right?
ddlMyList.SelectedValue = 3

Not so fast...
Thanks to the order of events in ASP.NET, the Button's Click event fires before the SqlDataSource is bound to the DropDownList.  .NET won't give you an error for that statement -- if there is a ListItem with a value of 3 in the datasource.  If there isn't, however, you get the dreaded "'ddlMyList' has a SelectedValue which is invalid because it does not exist in the list of items." error.
Since I can't check for the existence of 3 in the items list at the time the Click event fires, I can't prevent this.
What a ...

Go to the complete details ...