Using the RequiredFieldValidator Control The RequiredFieldValidator control enables you to require a user to enter a value into a form field before submitting the form. You must set two important properties when using the RequiredFieldValdiator control: The page in Listing 3.1 illustrates how you can use the RequiredFieldValidator control to require a user to enter both a first and last name (see Figure 3.5). Figure 3.5. Requiring a user to enter form field values. Listing 3.7. ShowRequiredFieldValidator.aspx <%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Show RequiredFieldValidator</title> </head> <body> <form runat="server"> <div> <asp:Label Text="First Name:" AssociatedControl Runat="server" /> <br /> <asp:TextBox Runat="server" /> <asp:RequiredFieldValidator ControlToValidate="txtFirstName" Text="(Required)" Runat="server" /> <br /><br /> <asp:Label Text="Last Name:" AssociatedControl Runat="server" /> <br /> <asp:TextBox Runat="server" /> <asp:RequiredFieldValidator ControlToValidate="txtLastName" Text="(Required)" Runat="server" /> <br /><br /> <asp:Button Text="Submit" Runat="server" /> </div> </form> </body> </html> | By default, the RequiredFieldValidator checks for a nonempty string (spaces don't count). If you enter anything into the form field associated with the RequiredFieldValidator, then the RequiredFieldValidator does not display its validation error message. You can use the RequiredFieldValidator control's InitialValue property to specify a default value other than an empty string. For example, the page in Listing 3.8 uses a RequiredFieldValidator to validate a DropDownList control (see Figure 3.6). Figure 3.6. Using a RequiredFieldValidator with a DropDownList control. Listing 3.8. ShowInitialValue.aspx <%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Page.IsValid Then lblResult.Text = dropFavoriteColor.SelectedValue End If End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Show Initial Value</title> </head> <body> <form runat="server"> <div> <asp:Label Text="Favorite Color:" AssociatedControl Runat="server" /> <br /> <asp:DropDownList Runat="server"> <asp:ListItem Text="Select Color" Value="none" /> <asp:ListItem Text="Red" Value="Red" /> <asp:ListItem Text="Blue" Value="Blue" /> <asp:ListItem Text="Green" Value="Green" /> </asp:DropDownList> <asp:RequiredFieldValidator Text="(Required)" InitialValue="none" ControlToValidate="dropFavoriteColor" Runat="server" /> <br /><br /> <asp:Button Text="Submit" Runat="server" OnClick="btnSubmit_Click" /> <hr /> <asp:Label Runat="server" /> </div> </form> </body> </html> | The first list item displayed by the DropDownList control displays the text "Select Color". If you submit the form without selecting a color from the DropDownList control, then a validation error message is displayed. Notice that the RequiredFieldValidator control includes an InitialValue property. The value of the first list from the DropDownList control is assigned to this property. |