Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

Asp.net control: Using the RequiredFieldValidator Control

by the3factory 3/11/2008 7:13:00 AM

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:

  • ControlToValidate The ID of the form field being validated.

  • Text The error message displayed when validation fails.

 

Demo1 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 id="Head1" runat="server">
            <title>Show RequiredFieldValidator</title>
            </head>
            <body>
            <form id="form1" runat="server">
            <div>
            <asp:Label
            id="lblFirstName"
            Text="First Name:"
            AssociatedControlID="txtFirstName"
            Runat="server" />
            <br />
            <asp:TextBox
            id="txtFirstName"
            Runat="server" />
            <asp:RequiredFieldValidator
            id="reqFirstName"
            ControlToValidate="txtFirstName"
            Text="(Required)"
            Runat="server" />
            <br /><br />
            <asp:Label
            id="lblLastName"
            Text="Last Name:"
            AssociatedControlID="txtLastName"
            Runat="server" />
            <br />
            <asp:TextBox
            id="txtLastName"
            Runat="server" />
            <asp:RequiredFieldValidator
            id="reqLastName"
            ControlToValidate="txtLastName"
            Text="(Required)"
            Runat="server" />
            <br /><br />
            <asp:Button
            id="btnSubmit"
            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 demo2 uses a RequiredFieldValidator to validate a DropDownList control.
Demo2. 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 id="Head1" runat="server">
            <title>Show Initial Value</title>
            </head>
            <body>
            <form id="form1" runat="server">
            <div>
            <asp:Label
            id="lblFavoriteColor"
            Text="Favorite Color:"
            AssociatedControlID="dropFavoriteColor"
            Runat="server" />
            <br />
            <asp:DropDownList
            id="dropFavoriteColor"
            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
            id="reqFavoriteColor"
            Text="(Required)"
            InitialValue="none"
            ControlToValidate="dropFavoriteColor"
            Runat="server" />
            <br /><br />
            <asp:Button
            id="btnSubmit"
            Text="Submit"
            Runat="server" OnClick="btnSubmit_Click" />
            <hr />
            <asp:Label
            id="lblResult"
            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.

Related posts

Sign up for PayPal and start accepting credit card payments instantly.


Powered by BlogEngine.NET 1.2.0.0