Group based validation (C#) : ValidationGroup « Validation « ASP.NET Tutorial






File: Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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 runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel Height="112px" 
                   ID="Panel1" 
                   runat="server" 
                   Width="392px">
            <asp:TextBox ID="TextBox1" 
                         runat="server" 
                         ValidationGroup="Group1"></asp:TextBox>
            <asp:RequiredFieldValidator ErrorMessage="*Required" 
                                        ID="RequiredFieldValidator1"
                                        runat="server" 
                                        ValidationGroup="Group1" 
                                        ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
            <asp:Button ID="Button1" runat="server" Text="Validate Group1" 
      ValidationGroup="Group1" OnClick="Button_Click"/></asp:Panel>&nbsp;<br />
            <asp:Panel Height="94px" 
                       ID="Panel2" 
                       runat="server" 
                       Width="392px" 
                       BorderWidth="2px">
            <asp:TextBox ID="TextBox2" runat="server" ValidationGroup="Group2"></asp:TextBox>
            <asp:RequiredFieldValidator ErrorMessage="*Required" 
                                        ID="RequiredFieldValidator2"
                                        runat="server" 
                                        ValidationGroup="Group2" 
                                        ControlToValidate="TextBox2">
                                        </asp:RequiredFieldValidator><br />
            <br />
            <asp:Button ID="Button2" 
                        runat="server" 
                        Text="Validate Group2" 
                        ValidationGroup="Group2" 
                        OnClick="Button_Click"/></asp:Panel>
        <asp:Label EnableViewState="False" ID="lblInfo" runat="server"></asp:Label><br />
    </div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ValidationGroups : System.Web.UI.Page
{
    protected void Button_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            lblInfo.Text = "Page posted back at " + DateTime.Now.ToLongTimeString();
        }
    
    }
}








8.12.ValidationGroup
8.12.1.A validation group enables you to group related form fields together.
8.12.2.Validation Groups Demo
8.12.3.Group based validation (C#)
8.12.4.Two validation groups
8.12.5.Using the ValidationGroup property (VB)