Archive for January 31st, 2008

Adding a blank / default item to an ASP.Net dropdown list and making it required

Here’s the declarative way to add a default first item to an ASP.Net drop down list:



Notice the important attribute: AppendDataBoundItems. This just tells ASP.Net to add your dynamically data-driven list items after any declarative markup items.

Now to make it required, you can just add a Required Field Validator:


ControlToValidate="ddCategory1" Text="* required">

Note, you just have to set your initial value to the value of your declarative first list item in the drop down. This tells the required field validator that this value doesn’t count and that the user haven’t selected an item yet.

Declarative markup is easy!