Asp.Net Required Field Validator for a DropDownList

A required field seems a bit odd for a dropdown list because it's got a value whether you like it or not. But when you feel that your dropdown list should have a required value it's usually because, you've given it an initial value of "--please select--" or somesuch and you want to validate that a 'real' value has been chosen.

Easy. Set the InitialValue property:

[sourcecode language="html"]
<asp:requiredfieldvalidator ControlToValidate="ddlMyPickList"
Text="*" InitialValue="-1"
ErrorMessage="You must choose a value"
runat="server" ID="rfvDdlMyPickList"/>
[/sourcecode]

Specifically, set it to the Value (not the Text) of the 'please select' item you added.

Asp.Net Required Field Validator for a D…

by Chris F Carroll read it in 1 min
0