9.2. Client-Side Evaluation


When you run this application, you will find that the error messages will not show as you move from field to field unless you either submit the page, or you set a value and then set it back to the original (e.g., you pick a book and then set the drop-down list back to the initial prompt). In either case, if you are using IE 4 or better, the form is not submitted. You can see this by opening the generated source code for the page, shown in Example 9-2.

Example 9-2. Validation page generated source code
 <script src="/books/2/660/1/html/2//Validators/WebResource.axd?a=s&amp;r=WebUIValidation. js&amp;t=632413796581225376" type="text/javascript"></script> <script type="text/javascript"> <!-- function WebForm_OnSubmit( ) { if (ValidatorOnSubmit( ) =  = false) return false; return true; } // --> </script> <script type="text/javascript"> <!-- var Page_Validators =  new Array(document.all["reqFieldBooks"], document. all["reqFieldEdition"], document.all["reqFieldBug"]); // --> </script> <script type="text/javascript"> <!-- var reqFieldBooks = document.all["reqFieldBooks"]; reqFieldBooks.controltovalidate = "ddlBooks"; reqFieldBooks.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid"; reqFieldBooks.initialvalue = "-- Please Pick A Book --"; var reqFieldEdition = document.all["reqFieldEdition"]; reqFieldEdition.controltovalidate = "rblEdition"; reqFieldEdition.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid"; reqFieldEdition.initialvalue = ""; var reqFieldBug = document.all["reqFieldBug"]; reqFieldBug.controltovalidate = "txtBug"; reqFieldBug.display = "Dynamic"; reqFieldBug.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid"; reqFieldBug.initialvalue = ""; // --> </script> 

You don't need to fully understand JavaScript to see that this auto-generated code is the client-side routines needed to validate the controls. If the controls fail validation, the page is never submitted, the error messages are displayed, and the user has another chance to correct the errors, as shown in Figure 9-7.

Figure 9-7. Client-side validation


This client-side validation saves you a roundtrip if the user has not entered valid data. If the data is valid on the source, the form will be submitted, and the validators will be checked again (to protect against spoofing on the client).

With downlevel browsers your code is unchanged, but the code sent to the client does not include the JavaScript. Because client-side validation will prevent your server-side event handlers from ever running if the control is not valid, you may want to force server-side validation. In that case, set a page attribute at the top of Default.aspx:

     <%@ Page Language="VB"     AutoEventWireup="false"     ClientTarget="downlevel"     CodeFile="Default.aspx.vb"     Inherits="Default_aspx" %> 

The ClientTarget="downlevel" directive will prevent the JavaScript from being sent to the client, even if your browser would have otherwise supported DHTML and client-side validation.



Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net