BaseValidator.IsValid Proprietà

Definizione

Ottiene o imposta un valore che indica se il controllo di input associato supera la convalida.

public:
 property bool IsValid { bool get(); void set(bool value); };
[System.ComponentModel.Browsable(false)]
public bool IsValid { get; set; }
[System.ComponentModel.Browsable(false)]
[System.Web.UI.Themeable(false)]
public bool IsValid { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.IsValid : bool with get, set
[<System.ComponentModel.Browsable(false)>]
[<System.Web.UI.Themeable(false)>]
member this.IsValid : bool with get, set
Public Property IsValid As Boolean

Valore della proprietà

true se il controllo di input associato supera la convalida; in caso contrario, false. Il valore predefinito è true.

Implementazioni

Attributi

Esempio

Nell'esempio di codice seguente viene illustrato come utilizzare la IsValid proprietà per determinare se il controllo di input associato supera la convalida.

Importante

L'esempio include una casella di testo che accetta l'input dell'utente e rappresenta quindi una potenziale minaccia alla sicurezza. Per impostazione predefinita, le pagine Web ASP.NET verificano che l'input dell'utente non includa script o elementi HTML. Per altre informazioni, vedere Cenni preliminari sugli attacchi tramite script.

<%@ 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 Button_Click(Object sender, EventArgs e) 
  {
    if (NumberCompareValidator.IsValid && TextBoxRequiredValidator.IsValid)
    {
      MessageLabel.Text = "Page submitted successfully.";
    }
    else
    {
      MessageLabel.Text = "There is an error on the page.";
    }
  }
 
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>Validator Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>Validator Example</h3>
     
      Enter a number from 1 to 10.
      <asp:textbox id="NumberTextBox" 
        runat="server"/>

      <asp:rangevalidator id="NumberCompareValidator" 
        controltovalidate="NumberTextBox"
        enableclientscript="False"  
        type="Integer"
        display="Dynamic" 
        errormessage="Please enter a value from 1 to 10."
        maximumvalue="10"
        minimumvalue="1"  
        text="*"
        runat="server"/>

      <asp:requiredfieldvalidator id="TextBoxRequiredValidator" 
        controltovalidate="NumberTextBox"
        enableclientscript="False"
        display="Dynamic" 
        errormessage="Please enter a value."
        text="*"
        runat="server"/>

      <br /><br />

      <asp:button id="SubmitButton"
        text="Submit"
        onclick="Button_Click"
        runat="server"/>
 
      <br /><br />
       
      <asp:label id="MessageLabel" 
        runat="server"/>

      <br /><br />

      <asp:validationsummary
        id="ErrorSummary"
        runat="server"/>
 
    </form>
  </body>
</html>
<%@ Page Language="VB" AutoEventWireup="False" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 
  Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SubmitButton.Click
 
    If NumberCompareValidator.IsValid And TextBoxRequiredValidator.IsValid Then
    
      MessageLabel.Text = "Page submitted successfully."
    
    Else
    
      MessageLabel.Text = "There is an error on the page."
    
    End If
    
  End Sub
 
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>Validator Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>Validator Example</h3>
     
      Enter a number from 1 to 10.
      <asp:textbox id="NumberTextBox" 
        runat="server"/>

      <asp:rangevalidator id="NumberCompareValidator" 
        controltovalidate="NumberTextBox"
        enableclientscript="False"  
        type="Integer"
        display="Dynamic" 
        errormessage="Please enter a value from 1 to 10."
        maximumvalue="10"
        minimumvalue="1"  
        text="*"
        runat="server"/>

      <asp:requiredfieldvalidator id="TextBoxRequiredValidator" 
        controltovalidate="NumberTextBox"
        enableclientscript="False"
        display="Dynamic" 
        errormessage="Please enter a value."
        text="*"
        runat="server"/>

      <br /><br />

      <asp:button id="SubmitButton"
        text="Submit"
        runat="server"/>
 
      <br /><br />
       
      <asp:label id="MessageLabel" 
        runat="server"/>

      <br /><br />

      <asp:validationsummary
        id="ErrorSummary"
        runat="server"/>
 
    </form>
  </body>
</html>

Commenti

Utilizzare la IsValid proprietà per determinare se il controllo di input associato supera la convalida.

Attenzione

Poiché il valore predefinito di questa proprietà è true, verrà restituito true se si esegue una query su questa proprietà prima dell'esecuzione della convalida. Ad esempio, questo problema può verificarsi se si tenta di utilizzare questa proprietà in Control.Load caso di pagina.

La IsValid proprietà viene valutata solo quando viene chiamato il Validate metodo . È possibile chiamare il Validate metodo per ogni controllo di convalida nella pagina singolarmente oppure chiamarli tutti contemporaneamente usando il Page.Validate metodo . I controlli pulsante con la relativa CausesValidation proprietà impostata su true chiameranno anche il Page.Validate metodo .

Nota

È possibile modificare manualmente il valore di questa proprietà dopo l'esecuzione della convalida. In questo modo è possibile eseguire l'override del risultato della convalida, se necessario.

La Page.IsValid proprietà per la pagina è impostata su true solo se anche la IsValid proprietà per ogni controllo di convalida nella pagina è impostata su true.

Questa proprietà non può essere impostata da temi oppure temi di fogli di stile. Per altre informazioni, vedere ThemeableAttribute e ASP.NET Temi e interfacce.

Si applica a

Vedi anche