Published: 01 Feb 2008
By:
Deepak Raghavan talks about the nuts and bolts of the ASP.NET Validation Library.
ASP.NET provides different options for validation at the client side. Each of the validation controls emits client side JavaScript behind the scenes for data validation. In the earlier versions of ASP.NET, the client side library was open for modification. Starting with ASP.NET 2.0, these functions are in the source file WebUIValidation.js which is included as an embedded resource within the System.Web assembly. This article looks at these internal functions and takes a deep dive approach to understand how they work with web controls during the page validation process.
In order to demonstrate the ASP.NET validation process, let us create a simple example using these quick steps to create a web application called "SimpleValidationTest":
TextBox1
), a required field validator (RequiredFieldValidator1
), and a button (Button1
) on the form.ValidationSummary1
) control.When an ASP.NET server side validation control is used, during the rendering phase it embeds in the HTML output some client side JavaScript code used to perform validation. All the ASP.NET validation controls inherit from BaseValidator
and the rendering of common functions happens within this class. When using any built in validation controls, these functions are always plugged into the rendered HTML source. The rendered functions are mentioned below along with a brief description.
Modified form tag: The first thing to note when using ASP.NET validation is that additional attributes are added to the form element. It is modified to execute the below JavaScript code for validation purposes.
The onsubmit
attribute is added during the rendering phase by the BaseValidator
class. Inside the OnPreRender
method, the common initialization tasks are invoked through the RegisterValidatorCommonScript
method. One of the steps is to render the submit script shown above, which is rendered via the Page.ClientScript.RegisterOnSubmitStatement
method.
Common JavaScript validation functions used across all validators are shown in listings 1 and 2 which can be seen in the rendered HTML source.
The function WebForm_OnSubmit
in listing 1, which is invoked when the form is posted, does not submit the form if the page is not valid. This condition is determined by the ValidatorOnSubmit
function, shown in listing 2.
The ValidatorOnLoad
function enumerates through the validators collection on the page to initialize each of them, and sets Page_ValidationActive
to true
. In turn, the ValidatorOnSubmit
function is executed since Page_ValidationActive
is true
. By default, Page_BlockSubmit
is set to false
and ValidatorCommonOnSubmit
returns !Page_BlockSubmit
. During client side Page validation, Page_BlockSubmit
is set to !Page_IsValid
. Thus, an invalid page turns Page_IsValid
to false
, setting the return value of ValidatorCommonOnScript
to false
. The page validation process is explained in more depth in the section entitled "Page validation in depth".
The JavaScript snippet shown in listing 3 is specific to the particular validators (RequiredFieldValidator1
and ValidationSummary1
) used in the page.
The above code snippet shows that the validator names are added to the Page_Validators
collection and that the validation summary control names are added to the Page_ValidationSummaries
collection. The main attributes of the validator - namely controltovalidate
, errormessage
, evaluationfunction
and initialvalue
are initialized. The value of the evaluationfunction
property is determined by the type of the server side validator. The possible values are shown in the following table.
Validator type | evaluationfunction |
---|---|
Required Field Validator | RequiredFieldValidatorEvaluateIsValid |
Range Validator | RangeValidatorEvaluateIsValid |
Custom Validator | CustomValidatorEvaluateIsValid |
Compare Validator | CompareValidatorEvaluateIsValid |
The validation functions listed above can be examined into much more detail if we can peek into WebUIValidation.js source file. There are different ways of achieving this and we are going to show a couple of approaches. The first approach is to peek into the System.Web.UI.WebControls assembly within the embedded resources. We do this with the help of the Reflector tool which uses .NET Reflection to generate the source code of any managed assembly. By opening .NET Reflector and navigating to System.Web, the JavaScript file WebUIValidation.js is found - located under the Resources folder. The second approach is to look at the source while debugging the code in the Visual Studio 2005 IDE. These steps would enable us to set breakpoints within the library and trace the code flow.
var
Page_ValidationVer
=
"125";
Earlier, we noted that Page_IsValid
determines whether a given page is valid or not. But which function sets this value and how is the function invoked? Using our sample example, we determine the answer to this question by inspecting the HTML rendered by the button control. By default, for every button control, the following attribute is added:
The onclick
attribute, along with the script, is added inside the AddAttributesToRender
method of the Button
control. The WebForm_DoPostBackWithOptions
function can be examined in the rendered web resource JavaScript file within Visual Studio 2005, as explained in the above section. This function is in the resource file named WebForms.js. The function takes an argument of type WebForm_PostBackOptions
. This function is also responsible for invoking the Page_ClientValidate
function, which sets the value of the Page_IsValid
member. The complete validation process triggered by the click of a button process can be summarized in the workflow below.
Page_IsValid
is setonSubmit
event is raisedWebForm_OnSubmit
ValidatorOnSubmit
ValidatorCommonOnSubmit
!Page_BlockSubmit
!(!Page_IsValid)
The above workflow returns true
if the page is valid, which in turn submits the form. It returns false
if the page is invalid, preventing the form from being submitted. In the latter case, the form displays the error messages under the validation summary control, due to the functions ValidatorUpdateIsValid
and ValidationSummaryOnSubmit
as shown in the first function reported in Listing 5.
These functions (Page_ClientValidate
and WebForm_DoPostBackWithOptions
), along with the definition of WebForm_PostBackOptions
as seen on Visual Studio 2005, are shown in listings 5, 6, and 7.
In the previous sections, we looked at the internals of the page validation process. In the following example, we are going to show a sample ASP.NET page where validation is turned on or off depending on a condition. Let us add another TextBox and custom validator to our sample application. This validation function determines whether the required field validator is enabled or not. The validateTextBox1
function takes in two arguments, src
and args
. When the value entered in TextBox2
is true
, RequiredFieldValidator1
is programmatically disabled. This behavior is achieved by leveraging the built-in ValidatorEnable
function, which takes in two arguments, the name of the validator and a Boolean flag to enable/disable it. The errormessage
attribute of the custom validator is dynamically changed within the code, depending on the user entry. The form is prevented from being submitted by setting args.IsValid
to false
. The complete source code is shown below.
The article explained the steps involved in validating and submitting a form in ASP.NET 2.0. Also, the main functions within the framework involved in this process were identified and addressed briefly. ASP.NET 2.0 has a rich client side framework, and understanding the internal implementation helps us to leverage and customize its behavior.