BaseValidator.ErrorMessage Proprietà

Definizione

Ottiene o imposta il testo del messaggio di errore visualizzato in un controllo ValidationSummary quando la convalida non riesce.

public:
 property System::String ^ ErrorMessage { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Bindable(true)]
public string ErrorMessage { get; set; }
public string ErrorMessage { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.ErrorMessage : string with get, set
member this.ErrorMessage : string with get, set
Public Property ErrorMessage As String

Valore della proprietà

Messaggio di errore visualizzato in un controllo ValidationSummary quando la convalida non riesce. Il valore predefinito è una stringa vuota (""), a indicare che questa proprietà non è impostata.

Implementazioni

Attributi

Esempio

Nell'esempio di codice seguente viene illustrato come usare la ErrorMessage proprietà per specificare messaggi diversi per il controllo e i ValidationSummaryRequiredFieldValidator controlli e CompareValidator .

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 (Page.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 Page.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

Quando si usa un ValidationSummary controllo, usare la ErrorMessage proprietà per specificare il testo da visualizzare nel ValidationSummary controllo quando la convalida non riesce per il controllo di convalida corrente. Per specificare il testo da visualizzare nel controllo di convalida stesso, usare la Text proprietà .

Nota

Se si imposta la proprietà senza impostare la ErrorMessageText proprietà, il valore della ErrorMessage proprietà viene visualizzato anche nel controllo di convalida.

Il valore di questa proprietà, quando impostato, può essere salvato automaticamente in un file di risorse usando uno strumento di progettazione. Per altre informazioni, vedere LocalizableAttributee ASP.NET Globalizzazione e localizzazione.

Si applica a

Vedi anche