ASP.NET Tutorial/Validation/ValidationGroup — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:57, 26 мая 2010

A validation group enables you to group related form fields together.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void btnLogin_Click(Object sender, EventArgs e)
    {
        if (Page.IsValid)
            lblLoginResult.Text = "Log in successful!";
    }
    void btnRegister_Click(Object sender, EventArgs e)
    {
        if (Page.IsValid)
            lblRegisterResult.Text = "Registration successful!";
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Validation Groups</title>
</head>
<body>
    <form id="form1" runat="server">
    <div class="column">
    <fieldset>
    <legend>Login</legend>
    <asp:Label
        id="lblUserName"
        Text="User Name:"
        AssociatedControlID="txtUserName"
        Runat="server" />
    <br />
    <asp:TextBox
        id="txtUserName"
        Runat="server" />
    <asp:RequiredFieldValidator
        id="reqUserName"
        ControlToValidate="txtUserName"
        Text="(Required)"
        ValidationGroup="LoginGroup"
        Runat="server" />
    <br /><br />
    <asp:Label
        id="lblPassword"
        Text="Password:"
        AssociatedControlID="txtPassword"
        Runat="server" />
    <br />
    <asp:TextBox
        id="txtPassword"
        TextMode="Password"
        Runat="server" />
    <asp:RequiredFieldValidator
        id="reqPassword"
        ControlToValidate="txtPassword"
        Text="(Required)"
        ValidationGroup="LoginGroup"
        Runat="server" />
    <asp:Button
        id="btnLogin"
        Text="Login"
        ValidationGroup="LoginGroup"
        Runat="server" OnClick="btnLogin_Click" />
    </fieldset>
    <asp:Label
        id="lblLoginResult"
        Runat="server" />
    </div>
    <div class="column">
    <fieldset>
    <legend>Register</legend>
    <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)"
        ValidationGroup="RegisterGroup"
        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)"
        ValidationGroup="RegisterGroup"
        Runat="server" />
    <br /><br />
    <asp:Button
        id="btnRegister"
        Text="Register"
        ValidationGroup="RegisterGroup"
        Runat="server" OnClick="btnRegister_Click" />
    </fieldset>
    <asp:Label
        id="lblRegisterResult"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Group based validation (C#)

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();
        }
    
    }
}


Two validation groups

<%@ page language="C#" %>
<script runat="server">
</script>
<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form runat="server">
        <h1>First form</h1>
        
            <asp:textbox id="TextBox1" runat="server" validationgroup="firstform">
            </asp:textbox>
            <asp:requiredfieldvalidator 
                 id="RequiredFieldValidator1" 
                 runat="server" 
                 display="Dynamic"
                 setfocusonerror="True" 
                 validationgroup="firstform" 
                 controltovalidate="TextBox1">***</asp:requiredfieldvalidator>
            <br />
            <br />
            <asp:button id="Button1" runat="server" validationgroup="firstform" text="Submit" />
        

        <h1>Second form</h1>
        
            <asp:textbox id="TextBox2" runat="server" validationgroup="secondform">
            </asp:textbox>
            <asp:requiredfieldvalidator 
                id="RequiredFieldValidator2" 
                runat="server" 
                display="Dynamic"
                setfocusonerror="True" 
                validationgroup="secondform" 
                controltovalidate="TextBox2">***</asp:requiredfieldvalidator>
            <br />
            <br />
            <asp:button id="Button2" runat="server" validationgroup="secondform" text="Submit" />            
        
    </form>
</body>
</html>


Using the ValidationGroup property (VB)

<%@ Page Language="VB" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Validation Groups</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>St. Louis .NET User Group</h1>
        Username:
        <asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>&nbsp; Password:
        <asp:TextBox ID="TextBox2" Runat="server" 
         TextMode="Password"></asp:TextBox>&nbsp;
        <asp:Button ID="Button1" Runat="server" Text="Login" 
         ValidationGroup="Login" />
            <br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" Runat="server" 
             ErrorMessage="* You must submit a username!"
             ControlToValidate="TextBox1" ValidationGroup="Login">
            </asp:RequiredFieldValidator>
            <br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" Runat="server" 
             ErrorMessage="* You must submit a password!"
             ControlToValidate="TextBox2" ValidationGroup="Login">
            </asp:RequiredFieldValidator>
        
        <h2>Sign-up for the newsletter!</h2>
        Email:
        <asp:TextBox ID="TextBox3" Runat="server"></asp:TextBox>&nbsp;
        <asp:Button ID="Button2" Runat="server" Text="Sign-up" 
         ValidationGroup="Newsletter" />&nbsp;
            <br />
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" 
             Runat="server"
             ErrorMessage="* You must submit a correctly formatted email address!"
             ControlToValidate="TextBox3" ValidationGroup="Newsletter"
             ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
            </asp:RegularExpressionValidator>
            <br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" Runat="server" 
             ErrorMessage="* You forgot your email address!"
             ControlToValidate="TextBox3" ValidationGroup="Newsletter">
            </asp:RequiredFieldValidator>
        
    </div>
    </form>
</body>
</html>


Validation Groups Demo

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Validation Groups Demo</title>
  </head>
  <body>
    <form id="Form1" runat="server">
        Username:<br />
        <asp:TextBox id="usernameTextBox" runat="server" />
        <asp:RequiredFieldValidator id="usernameReq" 
            runat="server"
            ControlToValidate="usernameTextBox"
            ErrorMessage="Username is required!"
            SetFocusOnError="True" 
            ValidationGroup="Login" />
        Password:<br />
        <asp:TextBox id="passwordTextBox" 
            runat="server"
            TextMode="Password" />
        <asp:RequiredFieldValidator id="passwordReq" 
            runat="server"
            ControlToValidate="passwordTextBox"
            ErrorMessage="Password is required!"
            SetFocusOnError="True" 
            ValidationGroup="Login" />
        <asp:Button ID="loginButton" 
            runat="server" 
            Text="Log In" 
            ValidationGroup="Login" />
      <h1>Register</h1>
        Username:<br />
        <asp:TextBox id="newUserNameTextBox" runat="server" />
        <asp:RequiredFieldValidator id="newUserNameReq" 
            runat="server"
            ControlToValidate="newUserNameTextBox"
            ErrorMessage="Username is required!"
            SetFocusOnError="True" 
            ValidationGroup="Register" />
        Password:<br />
        <asp:TextBox id="newPasswordTextBox" 
            runat="server"
            TextMode="Password" />
        <asp:RequiredFieldValidator id="newPasswordReq" 
            runat="server"
            ControlToValidate="newPasswordTextBox"
            ErrorMessage="Password is required!"
            SetFocusOnError="True" 
            ValidationGroup="Register" />
        <asp:Button ID="registerButton" 
            runat="server" 
            Text="Register" 
            ValidationGroup="Register" />
      
    </form>
  </body>
</html>