At times you want to return the default to show in an asp:DropDownList from the DataSource. Normally you have only two items:
I will illustrate how you can add and use a DataDefaultField (boolean) to select a specific item without needing to do any C# in the page.
The steps are:
- Create a class that inherits from DropDownList
- Add in two private variable, defaultValue, isPostBack
- Do an override as shown below to detect PostBacks
- Add a new Property
- Add another override (this picks up the (last) default value from the data.
- Add the last override which sets the default value IF it is not a postback
That’s it. You can now set the default value in the datasource.
Oops.. Bill just pointed out that I used extensions that I did not include.. Here there are: