This documentation is archived and is not being maintained.

Testing Validity Programmatically for ASP.NET Server Controls

Validation controls test user input, set an error state, and produce error messages. They do not change the flow of page processing — for example, they do not bypass your code if they detect a user input error. Instead, you test the state of the controls in your code before performing application-specific logic. If you detect an error, you prevent your own code from running; the page continues to process and is returned to the user with error messages.

Security Note   By default, the Web Forms page automatically validates that malicious users are not attempting to send script or HTML elements to your application. As long as this validation is enabled, you do not need to explicitly check for script or HTML elements in user input. For more information, see Scripting Exploits.

You can test for a general, page-wide state, and you can test the state of individual controls. Typically, you do these things in the event handlers that you create for a page.

Note   Validation information is not available during a page's initialization or load stage. However, you can call the Validate method manually during Page_Load and then test the IsValid property. For details about page states, see Web Forms Page Processing.

To test for a general error state

  • In your code, test the page's IsValid property. This property rolls up the values of the IsValid properties of all the validation controls on the page (using a logical AND); if any one validation control is set to invalid, the page's property will return false.

    The following example shows the event handler for a button. The code tests the IsValid property of the entire page. Note that there is no need for an else clause, because the page will be returned automatically to the browser and the validation controls will display their own error messages.

    ' Visual Basic
    Public Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) _
    Handles Button1.Click
       If Me.IsValid Then
          ' Perform database updates or other logic here
       End If
    End Sub
    
    // C#
    void Button1_Click(object sender, System.EventArgs e)
    {
       if (IsValid)
       {
          // Perform database updates or other logic here.
       }
    }
    

To test for the error state of individual controls

  • Loop through the page's Validators collection, which contains references to all the validation controls. You can then examine the IsValid property of each validation control.
    Note   If you want to perform this check during Page_Load, you must manually call the Validate method first.

    The following example shows how you can get information about individual validation controls.

    ' Visual Basic
    If (Me.IsPostBack) Then
       Me.Validate()
       If (Not Me.IsValid) Then
          Dim msg As String
          ' Loop through all validation controls to see which 
          ' generated the error(s).
          Dim oValidator As IValidator
          For Each oValidator In Validators
             If oValidator.IsValid = False Then
                msg = msg & "<BR>" & oValidator.ErrorMessage
             End If
          Next
          Label1.Text = msg
       End If
    End If
    
    // C#if (this.IsPostBack) 
    {
       this.Validate();
       if (!this.IsValid)
       {
          string msg = "";
          // Loop through all validation controls to see which
          // generated the errors.
          foreach (IValidator aValidator in this.Validators)
          {
             if (!aValidator.IsValid)
             {
                msg += "<br>" + aValidator.ErrorMessage;
             }
          }
          Label1.Text = msg;
       }
    

See Also

Introduction to Validating User Input in Web Forms | Controlling Validation Error Message Display for ASP.NET Server Controls | Validating Programmatically for ASP.NET Server Controls

Show: