Skip Navigation LinksHome : CBC Classes : ASP.Net : Validation Controls & Error Checking

CS218 ASP.Net - Validation and Error Checking

In this page we look at several ways of checking for errors, handling errors and making sure the user experiences your site without any issues or problems. In the first section you learn about the ASP.Net validation controls for performing client side error checking. You can also review the pure JavaScript method for doing client side error checking from the CSS class. In the second section you will learn about doing server side error checking using VB or C#. We don't cover this in great depth, because this is a subject that you should have learned in one of the programming classes. But I do have the code that you can copy and use as a model. In the last section you will learn how to setup your own custom error pages, so that the user never views a yellow screen of death.

  1. What is validation
  2. The required field validator
  3. Video Tutorial - required field validator
  4. The range field validator
  5. Video Tutorial - range field validator
  6. The regular expression validator
  7. Video Tutorial - regular expression validator
  8. Regular expression basics
  9. Regular expression test page
  10. The IQ Calculator demo
MSDN - Regular Expressions in ASP.NET

Server Side Error Checking

This code provides an example of using VB to do server side error checking.

    Protected Sub cmdCalcIQ_Click(sender As Object, e As System.EventArgs) Handles cmdCalcIQ.Click
        Dim shoe, age, IQ As Single
           shoe = Convert.toString(txtShoe.Text)
           age = Convert.toString(txtAge.Text)
           IQ = age * shoe
           lblIQ.text = IQ.toString
        Catch ex As Exception
            lblStatus.Text = "The following error occurred: " + ex.Message.ToString
            lblStatus.ForeColor = Drawing.Color.Red
        End Try
    End Sub

Custom Error Files

Add this section to the web.config for your web site.


    <customErrors mode="On" defaultRedirect="~/myErrorPage.htm">
      <error statusCode="404" redirect="my404ErrorPage.htm" />

It's probably a good idea to make any custom error file a .htm file instead of a .aspx file. If you make it a .aspx file it's possible that it won't be displayed. For example, if the error is being caused by a setting in your web.config file, then none of the .aspx pages can be displayed. On the other hand, .htm files can always be displayed.