The RequiredFieldValidator is a common utility in the asp.net coders validation toolkit. Its simple to use and probably represents one of the most common requirements for validation - that data must be there.
Its official definition is:
Evaluates the value of an input control to ensure that the user enters a value.
Using it to require TextBox content is an obvious and straightforward use but using it to validate DropDownList selections might not occur to you straight away.
How would you stop the user from submitting the form with "Please select" selected in the sample DropDownList below?
<asp:DropDownList ID="DropDownList1" runat="server" ValidationGroup="DropDownSample"> <asp:ListItem>Please select</asp:ListItem> <asp:ListItem>Lincolnshire</asp:ListItem> <asp:ListItem>Nottinghamshire</asp:ListItem> </asp:DropDownList>
InitialValue is the key. To use it you simply setup your RequiredFieldValidator as you normally would but add in the extra InitialValue attribute set to the string of text that you don't want to be submitted.
The RequiredFieldValidator for the DropDownList above would look something like this:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" InitialValue="Please select" ControlToValidate="DropDownList1" ErrorMessage="Please select a shire" />
A Real World Databound Example
The technique above is great for ensuring that your users properly select values for your DropDownLists. In the real world however you'll usually find yourself needing to bind the data to your drop down. So the issue becomes how do you combine your initial value text with the data that's bound?
There is an attribute called AppendDataBoundItems="True" which you can add to a DropDownList. This means that when you databind your dropdown the options you have hard coded in will not be overwritten.
So if you added the first starter ListItem with the text of "Please select" and databind your DropDownList it will be preserved at the top with your data below. This means you don't have to do any special tricks to get your default text included in your data set.
In the sample below I have included a datasource which randomly generates a list of numbers. Its a good example of how to create your own bindable data objects, but its not the focus of this article.
<asp:DropDownList ID="DropDownList2" runat="server" ValidationGroup="DataBoundDropDownSample" AppendDataBoundItems="True" DataSourceID="ObjectDataSource1"> <asp:ListItem>Please select</asp:ListItem> </asp:DropDownList> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select" TypeName="RunTingsProper.Sample.Data.SimpleIntegerData"> </asp:ObjectDataSource> <div> <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" InitialValue="Please select" ControlToValidate="DropDownList2" ValidationGroup="DataBoundDropDownSample" ErrorMessage="Please select a number" /></div> <asp:Button ID="Button2" runat="server" Text="Select" ValidationGroup="DataBoundDropDownSample" />
Download these examples
You can download these examples to play around with here:
Bonus tip - InitialValue can also be used for TextBox's
The text that you set in the InitialValue can also be used on other input controls. In theory you can use it to prevent any value that you like being entered into a TextBox. The TextBox doesn't have to have that value embedded at the start - it just can't be submitted with that value.
In practice this doesn't really come in useful very often; usually if you do need this kind of feature you can solve your problem more succinctly with a RegularExpressionValidator.
Bonus tip #2 - You can use more than one validator on a control
Don't forget that you can add more than one RequiredFieldValidator to work on a single control. This means that should you find yourself wanting to prevent more than one selection in a DropDownList you could wire up as many RequiredFieldValidators (each with their own InitialValue attributes) as you need to solve the problem.