Validation Controls in ASP.NET

The ASP.NET validation controls offer a great deal of flexibility and comfort when user wants to write data entry pages in web application.
  • 2506

The ASP.NET validation controls offer a great deal of flexibility and comfort when user wants to write data entry pages in web application. The validation groups combine the controls in a single page with a separate submit buttons for each group, so that you can submit each group individually. Each group can have separate validation controls.

To validate programmatically, you can call the Validate method overload which takes the validationGroup parameter to force validation for only that validation group. The IsValid property reflects the validity of all groups validated when you call the Validate method.

Design View

validate 1.gif
The page contains two text boxes to capture data from the user and two RequiredFieldValidator controls to ensure that the user does not leave a text box blank. When Button1 is clicked, the controls in validation group1 is validated and when Button2 is clicked, only the control in validation group2 is validated.

The following example demonstrates how to use the ValidationGroup property to specify the controls to validate when a button control posts back to the server.

Default.aspx

<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="ValidationGroups" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>ASP.NET Valiadtion Control</title
>
</head>
<
body>
 <form id="form1" runat="server">
   <div style="width: 298px">
     <asp:Panel Height="86px"
          ID="Panel1"
          runat="server"
          Width="272px" Font-Names="Verdana" Font-Size="Small">
          <b>Your Full Name</b>&nbsp;
          <asp:TextBox ID="TextBox1"
               runat="server"
               ValidationGroup="Group1" Width="138px"></asp:TextBox>
          <br />
          <asp:RequiredFieldValidator ErrorMessage="*Please enter name"
               ID="RequiredFieldValidator1"
               runat="server"
               ValidationGroup="Group1"
               ControlToValidate="TextBox1" ForeColor="Red"></asp:RequiredFieldValidator>
          <br />
          <asp:Button ID="Button1"
               runat="server"
               Text="Validate Group1"
               ValidationGroup="Group1"/>
           </asp:Panel> 
           &nbsp;<br />
           <asp:Panel Height="96px" ID="Panel2" runat="server" Width="295px"
           BorderWidth="0px" Font-Names="Verdana" Font-Size="Small">
               <b>Your Qualification </b>&nbsp;<asp:TextBox ID="TextBox2" runat="server" ValidationGroup="Group2"></asp:TextBox>
               <br />
           <asp:RequiredFieldValidator ErrorMessage="*Please enter qualifications"
                ID="RequiredFieldValidator2"
                runat="server"
                ValidationGroup="Group2"
                ControlToValidate="TextBox2" ForeColor="Red"></asp:RequiredFieldValidator>
           <br />
           <asp:Button ID="Button2"
                runat="server"
                Text="Validate Group2"
                ValidationGroup="Group2"/>
            </asp:Panel>
            <asp:Button ID="cmdValidateAll"
                 OnClick="cmdValidateAll_Click"
                 runat="server"
                 Text="Validate Summary" Font-Bold="True" Font-Names="Verdana"
                 Font-Size="Small" /><br />
            <br />
            <asp:Label EnableViewState="False" ID="Label1" runat="server"
                 Font-Names="Verdana" Font-Size="Small">
     </
asp:Label>
   </div>
 </form
>
</body>
</
html>

Default.aspx.vb

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Partial Public Class ValidationGroups
    Inherits System.Web.UI.Page
    Protected Sub cmdValidateAll_Click(ByVal sender As Object, ByVal e As EventArgs)
        Label1.Text = "Validation Status: " & Page.IsValid.ToString()
        Page.Validate("Group1")
        Label1.Text &= "<br /><br />Group1 Validition: " & Page.IsValid.ToString()
        Page.Validate("Group2")
        Label1.Text &= "<br />Group2 Validition: " & Page.IsValid.ToString()
    End
Sub
End Class

Output Window

validate 2.gif

If you left the textbox control blank like above, the RequiredField validator for that particular group alone is fired, and an error message will appear below the textbox. Click on the button validation summary to see the validation status of all controls. Also note that the value for Validation group is case sensitive.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.