Page.Validate 메서드

정의

페이지에 포함된 유효성 검사 컨트롤이 자신에게 할당된 정보의 유효성을 검사하도록 합니다.

오버로드

Validate()

페이지에 포함된 유효성 검사 컨트롤이 자신에게 할당된 정보의 유효성을 검사하도록 합니다.

Validate(String)

지정된 유효성 검사 그룹의 유효성 검사 컨트롤이 할당된 정보의 유효성을 검사하도록 합니다.

Validate()

페이지에 포함된 유효성 검사 컨트롤이 자신에게 할당된 정보의 유효성을 검사하도록 합니다.

public:
 virtual void Validate();
public virtual void Validate ();
abstract member Validate : unit -> unit
override this.Validate : unit -> unit
Public Overridable Sub Validate ()

예제

다음 코드 예제에서는 여러 다른 유효성 검사 그룹이 정의된 시나리오에서 페이지에서 메서드를 호출 Validate 합니다.

중요

이 예제에는 사용자 입력을 허용하는 텍스트 상자가 있으므로 보안상 위험할 수 있습니다. 기본적으로 ASP.NET 웹 페이지는 사용자 입력 내용에 스크립트 또는 HTML 요소가 포함되어 있지 않은지 확인합니다. 자세한 내용은 Script Exploits Overview를 참조하세요.

<%@ 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">

  protected void Button_Click(object sender, EventArgs e)
  {
    switch (((Button)sender).ID)
    {
      case "Button1":
        if (TextBoxValidator1.IsValid)
        {
          Label1.Text = "TextBox validates.";
        }
        else
        {
          Label1.Text = "";
          Label4.Text = "";
        }
        break;
      case "Button2":
        // Must explicitly cause Validate here because 
        // Button2 has CausesValidation set to false.
        Validate("Group2");
        if (CustomValidator.IsValid)
        {
          Label2.Text = "CheckBox validates.";
        }
        else
        {
          Label2.Text = "";
          Label4.Text = "";
        }
        break;
      default:
      Label1.Text = "";
      Label2.Text = "";
      break;
        
    }
  }

  // Custom validator for check box.
  protected void CustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
  {
    args.IsValid = (CheckBox1.Checked == true);
  }

  protected void Page_Load(object sender, EventArgs e)
  {
    if (IsPostBack && Context.Request.Form["__EVENTTARGET"] == "TextBox2")
    {
      // Handle AutoPostBack TextBox.
      Validate("Group3");
      if (Page.IsValid)
      {
        Label3.Text = "AutoPostBack TextBox validates.";
      }
      else
      {
        Label3.Text = "";
        Label4.Text = "";
      }
    }
    
  }

  protected void Button3_Click(object sender, EventArgs e)
  {
    Validate();
    if (Page.IsValid)
      Label4.Text = "All controls valid.";
    else
    {
      Label1.Text = "";
      Label2.Text = "";
      Label3.Text = "";
      Label4.Text = "";
    }

  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Page Validate</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      TextBox
      <asp:TextBox ID="TextBox1" ValidationGroup="Group1" runat="server"></asp:TextBox>
      <asp:RequiredFieldValidator Display="Static" ID="TextBoxValidator1" ValidationGroup="Group1" runat="server" ControlToValidate="TextBox1" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <br />
      CheckBox
      <asp:CheckBox ID="CheckBox1" ValidationGroup="Group2" runat="server" />
      <asp:CustomValidator ID="CustomValidator" ValidationGroup="Group2" runat="server" Text="(this option required)" OnServerValidate="CustomValidator_ServerValidate" EnableClientScript="False"></asp:CustomValidator>
      <br />
      <asp:Button ID="Button1" ValidationGroup="Group1" CausesValidation="true" runat="server" Text="Validate Group1 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label1" runat="server"></asp:Label>
      <br />
      <asp:Button ID="Button2" ValidationGroup="Group2" CausesValidation="false" runat="server" Text="Validate Group2 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label2" runat="server"></asp:Label>
      <br />
      <br />
      AutoPostBack TextBox
      <asp:TextBox AutoPostBack="true" ID="TextBox2" ValidationGroup="Group3" runat="server" CausesValidation="true"></asp:TextBox>
      <asp:RequiredFieldValidator ID="TextBoxValidator2" ValidationGroup="Group3" runat="server" ControlToValidate="TextBox2" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <asp:Label ID="Label3" runat="server"></asp:Label>
      <br />
      <br />
      <asp:Button ID="Button3" CausesValidation="true" runat="server" Text="Validate All Controls" OnClick="Button3_Click" />
      <asp:Label ID="Label4" runat="server"></asp:Label>
    
    </div>
    </form>
</body>
</html>
<%@ 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 Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Select Case CType(sender, Button).ID
      Case "Button1"
        If TextBoxValidator1.IsValid Then
          Label1.Text = "TextBox validates."
        Else
          Label1.Text = ""
          Label4.Text = ""
        End If
      Case "Button2"
        ' Must explicitly cause Validate here because 
        ' Button2 has CausesValidation set to false.
        Validate("Group2")
        If CustomValidator.IsValid Then
          Label2.Text = "CheckBox validates."
        Else
          Label2.Text = ""
          Label4.Text = ""
        End If
      Case Else
        Label1.Text = ""
        Label2.Text = ""
    End Select

  End Sub
  
  Protected Sub CustomValidator_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
     
    args.IsValid = (CheckBox1.Checked)

  End Sub
  
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    If (IsPostBack) Then
      
    ' Handle AutoPostBack TextBox.
      Validate("Group3")
      If (Page.IsValid) Then
        Label3.Text = "AutoPostBack TextBox validates."
      Else
        Label3.Text = ""
        Label4.Text = ""
      End If
    End If
    
  End Sub

  Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  
    Validate()
    If (Page.IsValid) Then
      Label4.Text = "All controls valid."
    Else
      Label1.Text = ""
      Label2.Text = ""
      Label3.Text = ""
      Label4.Text = ""
    End If
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Page Validate</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      TextBox
      <asp:TextBox ID="TextBox1" ValidationGroup="Group1" runat="server"></asp:TextBox>
      <asp:RequiredFieldValidator Display="Static" ID="TextBoxValidator1" ValidationGroup="Group1" runat="server" ControlToValidate="TextBox1" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <br />
      CheckBox
      <asp:CheckBox ID="CheckBox1" ValidationGroup="Group2" runat="server" />
      <asp:CustomValidator ID="CustomValidator" ValidationGroup="Group2" runat="server" Text="(this option required)" OnServerValidate="CustomValidator_ServerValidate" EnableClientScript="False"></asp:CustomValidator>
      <br />
      <asp:Button ID="Button1" ValidationGroup="Group1" CausesValidation="true" runat="server" Text="Validate Group1 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label1" runat="server"></asp:Label>
      <br />
      <asp:Button ID="Button2" ValidationGroup="Group2" CausesValidation="false" runat="server" Text="Validate Group2 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label2" runat="server"></asp:Label>
      <br />
      <br />
      AutoPostBack TextBox
      <asp:TextBox AutoPostBack="true" ID="TextBox2" ValidationGroup="Group3" runat="server" CausesValidation="true"></asp:TextBox>
      <asp:RequiredFieldValidator ID="TextBoxValidator2" ValidationGroup="Group3" runat="server" ControlToValidate="TextBox2" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <asp:Label ID="Label3" runat="server"></asp:Label>
      <br />
      <br />
      <asp:Button ID="Button3" CausesValidation="true" runat="server" Text="Validate All Controls" OnClick="Button3_Click" />
      <asp:Label ID="Label4" runat="server"></asp:Label>
    
    </div>
    </form>
</body>
</html>

설명

이 메서드는 사용자가 기본값인 속성이 설정된 ASP.NET 서버 컨트롤 CausesValidation 을 클릭할 true때 호출됩니다. 여기에는 , ImageButton및 웹 서버 컨트롤, HtmlInputButton, HtmlInputImage및 HTML 서버 컨트롤 및 HtmlButton 컨트롤과 같은 CheckBoxTextBox서버에 자동으로 다시 게시할 수 있는 컨트롤이 BulletedList ListControl포함 ButtonLinkButton 됩니다.

페이지의 단추 컨트롤에 대한 유효성 검사를 사용하지 않으려면 단추 컨트롤의 CausesValidation 속성을 .로 false설정합니다.

이 메서드가 호출되면 속성과 연결된 Page.Validators 개체에 ValidatorCollection 포함된 유효성 검사 컨트롤을 반복하고 현재 유효성 검사 그룹의 각 유효성 검사 컨트롤에 대한 유효성 검사 논리를 호출합니다. 유효성 검사 그룹은 페이지를 서버에 게시한 컨트롤에 의해 결정됩니다. 유효성 검사 그룹을 지정하지 않으면 유효성 검사 그룹이 사용되지 않습니다.

참고

페이지 유효성 검사의 동작이 변경되었습니다. ASP.NET 2.0에서 컨트롤은 더 이상 메서드를 Page.Validate() 호출하지 않으며 대신 메서드를 Page.Validate(String) 사용합니다. ASP.NET 2.0 페이지에서 메서드를 사용하는 Page.Validate() 경우 유효성 검사 그룹이 무시되고 모든 컨트롤의 유효성이 검사됩니다.

상속자 참고

메서드 Validate() 는 ASP.NET 2.0에서 사용되지 않습니다. ASP.NET 2.0을 사용하는 경우 페이지 유효성 검사 동작을 변경하도록 메서드를 재정의 Validate(String) 합니다.

추가 정보

적용 대상

Validate(String)

지정된 유효성 검사 그룹의 유효성 검사 컨트롤이 할당된 정보의 유효성을 검사하도록 합니다.

public:
 virtual void Validate(System::String ^ validationGroup);
public virtual void Validate (string validationGroup);
abstract member Validate : string -> unit
override this.Validate : string -> unit
Public Overridable Sub Validate (validationGroup As String)

매개 변수

validationGroup
String

유효성을 검사할 컨트롤의 유효성 검사 그룹 이름입니다.

예제

다음 코드 예제에서는 여러 다른 유효성 검사 그룹이 정의된 시나리오에서 페이지에서 메서드를 호출 Validate 합니다.

중요

이 예제에는 사용자 입력을 허용하는 텍스트 상자가 있으므로 보안상 위험할 수 있습니다. 기본적으로 ASP.NET 웹 페이지는 사용자 입력 내용에 스크립트 또는 HTML 요소가 포함되어 있지 않은지 확인합니다. 자세한 내용은 Script Exploits Overview를 참조하세요.

<%@ 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">

  protected void Button_Click(object sender, EventArgs e)
  {
    switch (((Button)sender).ID)
    {
      case "Button1":
        if (TextBoxValidator1.IsValid)
        {
          Label1.Text = "TextBox validates.";
        }
        else
        {
          Label1.Text = "";
          Label4.Text = "";
        }
        break;
      case "Button2":
        // Must explicitly cause Validate here because 
        // Button2 has CausesValidation set to false.
        Validate("Group2");
        if (CustomValidator.IsValid)
        {
          Label2.Text = "CheckBox validates.";
        }
        else
        {
          Label2.Text = "";
          Label4.Text = "";
        }
        break;
      default:
      Label1.Text = "";
      Label2.Text = "";
      break;
        
    }
  }

  // Custom validator for check box.
  protected void CustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
  {
    args.IsValid = (CheckBox1.Checked == true);
  }

  protected void Page_Load(object sender, EventArgs e)
  {
    if (IsPostBack && Context.Request.Form["__EVENTTARGET"] == "TextBox2")
    {
      // Handle AutoPostBack TextBox.
      Validate("Group3");
      if (Page.IsValid)
      {
        Label3.Text = "AutoPostBack TextBox validates.";
      }
      else
      {
        Label3.Text = "";
        Label4.Text = "";
      }
    }
    
  }

  protected void Button3_Click(object sender, EventArgs e)
  {
    Validate();
    if (Page.IsValid)
      Label4.Text = "All controls valid.";
    else
    {
      Label1.Text = "";
      Label2.Text = "";
      Label3.Text = "";
      Label4.Text = "";
    }

  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Page Validate</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      TextBox
      <asp:TextBox ID="TextBox1" ValidationGroup="Group1" runat="server"></asp:TextBox>
      <asp:RequiredFieldValidator Display="Static" ID="TextBoxValidator1" ValidationGroup="Group1" runat="server" ControlToValidate="TextBox1" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <br />
      CheckBox
      <asp:CheckBox ID="CheckBox1" ValidationGroup="Group2" runat="server" />
      <asp:CustomValidator ID="CustomValidator" ValidationGroup="Group2" runat="server" Text="(this option required)" OnServerValidate="CustomValidator_ServerValidate" EnableClientScript="False"></asp:CustomValidator>
      <br />
      <asp:Button ID="Button1" ValidationGroup="Group1" CausesValidation="true" runat="server" Text="Validate Group1 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label1" runat="server"></asp:Label>
      <br />
      <asp:Button ID="Button2" ValidationGroup="Group2" CausesValidation="false" runat="server" Text="Validate Group2 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label2" runat="server"></asp:Label>
      <br />
      <br />
      AutoPostBack TextBox
      <asp:TextBox AutoPostBack="true" ID="TextBox2" ValidationGroup="Group3" runat="server" CausesValidation="true"></asp:TextBox>
      <asp:RequiredFieldValidator ID="TextBoxValidator2" ValidationGroup="Group3" runat="server" ControlToValidate="TextBox2" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <asp:Label ID="Label3" runat="server"></asp:Label>
      <br />
      <br />
      <asp:Button ID="Button3" CausesValidation="true" runat="server" Text="Validate All Controls" OnClick="Button3_Click" />
      <asp:Label ID="Label4" runat="server"></asp:Label>
    
    </div>
    </form>
</body>
</html>
<%@ 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 Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Select Case CType(sender, Button).ID
      Case "Button1"
        If TextBoxValidator1.IsValid Then
          Label1.Text = "TextBox validates."
        Else
          Label1.Text = ""
          Label4.Text = ""
        End If
      Case "Button2"
        ' Must explicitly cause Validate here because 
        ' Button2 has CausesValidation set to false.
        Validate("Group2")
        If CustomValidator.IsValid Then
          Label2.Text = "CheckBox validates."
        Else
          Label2.Text = ""
          Label4.Text = ""
        End If
      Case Else
        Label1.Text = ""
        Label2.Text = ""
    End Select

  End Sub
  
  Protected Sub CustomValidator_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
     
    args.IsValid = (CheckBox1.Checked)

  End Sub
  
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    If (IsPostBack) Then
      
    ' Handle AutoPostBack TextBox.
      Validate("Group3")
      If (Page.IsValid) Then
        Label3.Text = "AutoPostBack TextBox validates."
      Else
        Label3.Text = ""
        Label4.Text = ""
      End If
    End If
    
  End Sub

  Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  
    Validate()
    If (Page.IsValid) Then
      Label4.Text = "All controls valid."
    Else
      Label1.Text = ""
      Label2.Text = ""
      Label3.Text = ""
      Label4.Text = ""
    End If
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Page Validate</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      TextBox
      <asp:TextBox ID="TextBox1" ValidationGroup="Group1" runat="server"></asp:TextBox>
      <asp:RequiredFieldValidator Display="Static" ID="TextBoxValidator1" ValidationGroup="Group1" runat="server" ControlToValidate="TextBox1" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <br />
      CheckBox
      <asp:CheckBox ID="CheckBox1" ValidationGroup="Group2" runat="server" />
      <asp:CustomValidator ID="CustomValidator" ValidationGroup="Group2" runat="server" Text="(this option required)" OnServerValidate="CustomValidator_ServerValidate" EnableClientScript="False"></asp:CustomValidator>
      <br />
      <asp:Button ID="Button1" ValidationGroup="Group1" CausesValidation="true" runat="server" Text="Validate Group1 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label1" runat="server"></asp:Label>
      <br />
      <asp:Button ID="Button2" ValidationGroup="Group2" CausesValidation="false" runat="server" Text="Validate Group2 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label2" runat="server"></asp:Label>
      <br />
      <br />
      AutoPostBack TextBox
      <asp:TextBox AutoPostBack="true" ID="TextBox2" ValidationGroup="Group3" runat="server" CausesValidation="true"></asp:TextBox>
      <asp:RequiredFieldValidator ID="TextBoxValidator2" ValidationGroup="Group3" runat="server" ControlToValidate="TextBox2" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <asp:Label ID="Label3" runat="server"></asp:Label>
      <br />
      <br />
      <asp:Button ID="Button3" CausesValidation="true" runat="server" Text="Validate All Controls" OnClick="Button3_Click" />
      <asp:Label ID="Label4" runat="server"></asp:Label>
    
    </div>
    </form>
</body>
</html>

설명

이 메서드는 사용자가 기본값인 속성이 설정된 ASP.NET 서버 컨트롤 CausesValidation 을 클릭할 true때 호출됩니다. 여기에는 , ImageButton및 웹 서버 컨트롤, HtmlInputButton, HtmlInputImage및 HTML 서버 컨트롤 및 HtmlButton 컨트롤과 같은 CheckBoxTextBox서버에 자동으로 다시 게시할 수 있는 컨트롤이 BulletedList ListControl포함 ButtonLinkButton 됩니다.

페이지의 단추 컨트롤에 대한 유효성 검사를 사용하지 않으려면 단추 컨트롤의 CausesValidation 속성을 .로 false설정합니다.

이 메서드는 Validate 지정된 유효성 검사 그룹의 유효성을 검사합니다. 유효성 검사 그룹에서 IsValid 메서드를 호출 Validate 한 후에는 지정된 유효성 검사 그룹과 페이지를 서버에 게시한 컨트롤의 유효성 검사 그룹이 모두 유효한 경우에만 메서드가 반환 true 됩니다.

추가 정보

적용 대상