The problem of default values in ASP.NET dropdowns
JM Alarcon
ASP.NET Visual Developer MVP
MsC Mechanical engineering and business consultant specialist, he has written several books and hundreds of articles on computer science and engineering in specialized press such as PC World, Windows Magazine, dotNetMania... JM is also a Microsoft Certified Trainer (MCT).
This is a simple trick, but very interesting and useful.
The problem comes up when we use any type of DataSource control (ObjectDataSource, SqlDataSource, LinqDataSource, etc…) bound to a DropDownList to show some options to be selected, for example, a list of products categories to be displayed in another control like a GridView. We’ll get only the values for available categories in the database from the data source, but we’ll usually want to show a couple of default additional values (besides specific categories) such as “All categories” or “None”. How can we do it?
It’s actually easy: using the AppendDataBoundItems property of DropDownList control.
If we set this property to True, the control will preserve in the list any element we added in design mode even if we add other elements dynamically in run time with the data source. So we can have more options in our dropdowns just by adding a couple of elements as in the example, according to your needs.
This is simple and useful!



