When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Monday, April 16, 2001

Preparing your ASP Pages for a Transition to ASP.NET, Part 3

By Darren Neimke and Scott Mitchell

  • Read Part 1
  • Read Part 2

  • In Part 2 we looked at the event life-cycle of an ASP.NET Web page and discussed creating a code template for our ASP pages. We also dissected the page-level events that occur in an ASP.NET page and looked at how these can be implemented in our ASP code template. In this part we'll examine how to populate those two important ASP.NET page-level properties, IsValid and IsPostBack, in our ASP code template.

    - continued -

    Page-Level Properties for our Code Template
    As mentioned earlier, the ASP.NET Page object also has some useful properties that developers will often check in the Page_Load event handler. The first of these properties is the IsPostBack property. This Boolean property is set to True if the page has been posted back to itself, an action that typically occurs after the user clicks on a submit button. If the page has not been posted back, IsPostBack is False.

    Our classic ASP code template also contains its own version of the IsPostBack property. This will make the migration of our ASP code to ASP.NET that much easier. The following code snippet shows the creation of our IsPostBack property (named blnIsPostBack for our code template) and how we determine if, in fact, the page has been posted back or not:

    Dim blnIsPostBack
    'Check to see if the user has arrived at this page by 
    'submitting back from the form.
    If isEmpty(Request("cmdSubmit")) Then
       blnIsPostBack = False
       blnIsPostBack = True
    End If

    The other useful ASP.NET property is IsValid, whose value is based on the validity of the values entered by the user in the form field elements of the form that are tied to various ASP.NET validation controls. If all of the controls report that they are valid, IsValid is True; if any of the validation controls are invalid, however, IsValid is False. (To learn more about form validation with ASP.NET be sure to read: Form Validation with ASP.NET: It Doesn't Get Any Easier!)

    There are five types of validation controls that ship with ASP.NET:

    ASP.NET Validation Controls
    Validation Control Description
    RequiredFieldValidator This validator ensures that the user enters a value into a particular form field.
    CompareValidator Compares a user's entry against a constant value or the value of another Web control using a comparison operator (less than, equal, greater than, and so on).
    RangeValidator Checks that a user's entry is between specified lower and upper boundaries. You can check ranges within pairs of numbers, alphabetic characters, and dates.
    RegularExpressionValidator Checks that the entry matches a pattern defined by a regular expression (to learn more about regular expressions be sure to check out the: Regular Expressions Article Index). This type of validation allows you to check for predictable sequences of characters, such as those in social security numbers, e-mail addresses, telephone numbers, postal codes, and so on.
    CustomValidator Checks the user's entry against validation logic that you code yourself.

    While it is beyond the scope of this article to further articulate how ASP.NET's validation controls work, know that if you tie a Web control (such as a text box) to a RequiredFieldValidator validation control and the user doesn't fill in a value for that text box, then the Page.IsValid property would be set to False because a validation control would have reported that its form field was invalid.

    The advantage of having an IsValid property is that you only need to query the value of one property to determine whether or not to continue along with page processing. For example, if you are creating an ASP.NET page to insert the user's responses into a database, if they failed to enter some important piece of information you could refrain from entering the information into the database and, instead, prompt the user with an error message asking them to enter that bit of information and resubmit the form. (For this to work, of course, the value of the IsValid property must be set before the Page_Load event handler fires.)

    We will replicate the functionality of the IsValid property in our ASP code template like so:

    Dim blnIsValid, strValidationMessage
    Private function ValidateFormValues()
    ' This function validates the user entry if the page is being posted back 
    ' and returns True if the data validates and False if there are errors.
        ' Grab the values for the fields that we wish to validate.
        FirstName	= Request("First_Name")
        ' Compile a validation message.
        strValidationMessage = strValidationMessage & _
                        RequiredField(FirstName, "FirstName")
        ' Check to see if errors occurred.	
        if len( strValidationMessage ) < 1 then
           ValidateFormValues = True
           ValidateFormValues = False
        end if
    end function
    ' Get a value for our Page.IsValid property a central Validator function
    blnIsValid = ValidateFormValues()

    Notice that the ValidateFormValues function utilizes a function called RequiredField(). The idea here is to create a generic function in our code template for each type of ASP.NET validator control. From the ValidateFormValues function we simply pass each form field to be validated off to it's respective validator function with the specialized set of parameters that that validator function requires. For example one of the expected Parameters of the RegularExpressionValidator would be the regular expression pattern. These validator functions are examined in Part 4.

  • Read Part 4!

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article