Page.Validate Yöntem

Tanım

Sayfaya dahil edilen tüm doğrulama denetimlerinden atanan bilgileri doğrulamalarını sağlar.

Aşırı Yüklemeler

Validate()

Sayfaya dahil edilen tüm doğrulama denetimlerinden atanan bilgileri doğrulamalarını sağlar.

Validate(String)

Belirtilen doğrulama grubundaki doğrulama denetimlerine atanan bilgilerini doğrulamalarını belirtir.

Validate()

Sayfaya dahil edilen tüm doğrulama denetimlerinden atanan bilgileri doğrulamalarını sağlar.

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

Örnekler

Aşağıdaki kod örneği, tanımlanan birkaç farklı doğrulama grubuna sahip bir senaryoda bir sayfadaki yöntemini çağırır Validate .

Önemli

Bu örnekte, olası bir güvenlik tehdidi olan kullanıcı girişini kabul eden bir metin kutusu vardır. Varsayılan olarak, ASP.NET Web sayfaları kullanıcı girişinin betik veya HTML öğeleri içermediğini doğrular. Daha fazla bilgi için bkz . Betik Açıklarına Genel Bakış.

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

Açıklamalar

Bu yöntem, kullanıcı özelliği varsayılan olarak ayarlanmış ASP.NET sunucu denetimine CausesValidation truetıkladığında çağrılır. Bunlar , , ImageButtonve Web sunucusu denetimlerini, HtmlInputButton, HtmlInputImage, ve HtmlButton HTML sunucusu denetimlerini ve , CheckBox, ve BulletedList ListControldenetimleri gibi sunucuya otomatik olarak TextBoxgeri gönderebilen denetimleri içerirButtonLinkButton.

Sayfadaki herhangi bir düğme denetiminde doğrulamayı devre dışı bırakmak için düğme denetiminin CausesValidation özelliğini olarak falseayarlayın.

Bu yöntem çağrıldığında, özelliğiyle Page.Validators ilişkili nesnede bulunan doğrulama denetimlerinde ValidatorCollection yinelenir ve geçerli doğrulama grubundaki her doğrulama denetimi için doğrulama mantığını çağırır. Doğrulama grubu, sayfayı sunucuya postalayan denetim tarafından belirlenir. Doğrulama grubu belirtilmezse, doğrulama grubu kullanılmaz.

Not

Sayfa doğrulama davranışı değişti. ASP.NET 2.0'da denetimler artık yöntemini çağırmaz Page.Validate() ; bunun yerine yöntemini kullanır Page.Validate(String) . yöntemini bir ASP.NET 2.0 sayfasında kullanırsanız Page.Validate() doğrulama grupları yoksayılır ve tüm denetimler doğrulanır.

Devralanlara Notlar

yöntemi Validate() ASP.NET 2.0 tarafından kullanılmaz. ASP.NET 2.0 kullanırken, sayfa doğrulama davranışını değiştirmek için yöntemini geçersiz kılın Validate(String) .

Ayrıca bkz.

Şunlara uygulanır

Validate(String)

Belirtilen doğrulama grubundaki doğrulama denetimlerine atanan bilgilerini doğrulamalarını belirtir.

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)

Parametreler

validationGroup
String

Doğrulanması gereken denetimlerin doğrulama grubu adı.

Örnekler

Aşağıdaki kod örneği, tanımlanan birkaç farklı doğrulama grubuna sahip bir senaryoda bir sayfadaki yöntemini çağırır Validate .

Önemli

Bu örnekte, olası bir güvenlik tehdidi olan kullanıcı girişini kabul eden bir metin kutusu vardır. Varsayılan olarak, ASP.NET Web sayfaları kullanıcı girişinin betik veya HTML öğeleri içermediğini doğrular. Daha fazla bilgi için bkz . Betik Açıklarına Genel Bakış.

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

Açıklamalar

Bu yöntem, kullanıcı özelliği varsayılan olarak ayarlanmış ASP.NET sunucu denetimine CausesValidation truetıkladığında çağrılır. Bunlar , , ImageButtonve Web sunucusu denetimlerini, HtmlInputButton, HtmlInputImage, ve HtmlButton HTML sunucusu denetimlerini ve , CheckBox, ve BulletedList ListControldenetimleri gibi sunucuya otomatik olarak TextBoxgeri gönderebilen denetimleri içerirButtonLinkButton.

Sayfadaki herhangi bir düğme denetiminde doğrulamayı devre dışı bırakmak için düğme denetiminin CausesValidation özelliğini olarak falseayarlayın.

yöntemi belirtilen Validate doğrulama grubunu doğrular. Yöntemi bir doğrulama grubunda çağırdıktan Validate sonra, IsValid yöntem yalnızca hem belirtilen doğrulama grubu hem de sayfanın sunucuya nakledilmesine neden olan denetimin doğrulama grubu geçerliyse döndürülecektir true .

Ayrıca bkz.

Şunlara uygulanır