Preparing your ASP Pages for a Transition to ASP.NET, Part 4By Darren Neimke and Scott Mitchell
In Part 3 we looked at how to populate the
blnIsPostBack variables in our ASP code template. In this part we'll look at the validator
functions we need in order to successfully determine whether or not
blnIsValid should be True or
False. We'll also look at an example application of this code template and wrap up the article!
Creating the Validator Functions
So far I (Darren Neimke) have written three specific validator functions that I use in this code template. After creating these functions be sure to copy them to an include file,
(To learn more about server-side includes be sure to read: The Low-Down on
#include.) You are invited to create your own validation functions for this ASP code template.
These three validation functions should get you started:
Take a moment to check out this example script. The example encorporates all of the elements of the migration code template we've discussed in this article. As you poke through the example note that it is comprised of the following "sections:"
- Global variable definitions - At the beginning of the example, a number of global variables are
defined. These include the
blnIsValidvariables that we discussed earlier as well as a variable for each form field.
- The inclusion of
Validators.asp- this include file contains the validator functions, which are used in the
blnIsValidtests - These code blocks are nearly identical to the examples we looked at earlier in this article for determining the values of these two properties. It simply determines whether or not the page has been posted back to and, if it has, checks to see if all the validation controls are valid.
Page_Loadfunction - Note that this function checks both the
blnIsValidproperties and can take various actions based on the values of these properties.
ValidateFormValuesfunction - This function needs to be altered for each page, depending on the validation requirements for each form field value.
- The call to the
Page_Loadfunction - Note the
Call Page_Load()line of code - this fires off the whole pseudo-event life-cycle of the code template page.
- A postback form - A postback form can be created by simply leaving off the
ACTIONparameter in the HTML
That's it! I would recommend that you adopt a template similar to this when creating classic ASP pages.
Doing so will make the migration to ASP.NET that much easier, both from a code maintenance and code style point of
view. When I say code style point of view, what I mean is that you will already be thinking
in terms of coding against the
Page object and its events, the style employed by ASP.NET.
Adopting this template will save you time when you decide to migrate your Web site to ASP.NET and will help
gently familiarize you with some of ASP.NET's stylistic and code differences.