Tuesday, June 24, 2008

Required Field Validator for DropDownList

You can use RequiredFieldValidator for DropDownList.

Just select InitialValue of the RequiredFieldValidator to -1 or 0 whatever that you select for the first element (often with text "Please Select") of DropDownList. Now validator will work and you need to select any other value than the InitialValue in order to pass validation.

Tuesday, June 17, 2008

Default Item for the DropDownList

You can bind C# dropdownlist to datasource and then assign the default value to it using following syntax. By default the 0th item is selected but you can specify it explicitly if you want to select item at different location.

dropDownList.Items.Insert(0,"Please Select"); // Inserts at the first location
dropDownList.Items[0].Selected = true; // First location is selected