data matrix barcode c# Using Validator Controls in C#

Painting Data Matrix ECC200 in C# Using Validator Controls

Using Validator Controls
ECC200 Maker In C#.NET
Using Barcode printer for VS .NET Control to generate, create Data Matrix 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Decoding Data Matrix In C#
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Because the RequiredFieldValidator control is the first of the validator controls we ll look at in this section, it s useful to review the class hierarchy for the RequiredFieldValidator class (shown in Figure 5-3). We know that everything in the .NET Framework descends from Object, so it s no surprise that the most remote descendant of the RequiredFieldValidator class is Object.
Bar Code Printer In Visual C#
Using Barcode maker for Visual Studio .NET Control to generate, create barcode image in .NET applications.
www.OnBarcode.com
Barcode Decoder In C#
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Figure 5-3 : Object class hierarchy for RequiredFieldValidator in the .NET Framework The RequiredFieldValidator Control To understand this hierarchy, it s useful to see the RequiredFieldValidator control at work. Login.aspx, shown in Listing 5-1, uses the RequiredFieldValidator control. Figure 5-4 shows Login.aspx and what happens when we submit the form with neither field filled in.
ECC200 Drawer In VS .NET
Using Barcode creation for ASP.NET Control to generate, create Data Matrix ECC200 image in ASP.NET applications.
www.OnBarcode.com
DataMatrix Generator In .NET
Using Barcode printer for .NET Control to generate, create Data Matrix image in .NET applications.
www.OnBarcode.com
Figure 5-4 : The Login.aspx page when the Login button is clicked and the fields are not filled in The form validator controls all have an ErrorMessage attribute that in this example has been set to * , and thus an asterisk is displayed next to any field that doesn t validate properly. The RequiredFieldValidator control is perhaps the simplest validator control it checks to see whether a field has a value. How does a validator control work That depends on several factors. As with many aspects of ASP.NET, it s useful to look at the HTML code that the browser sees, to determine how your request for a validator control is translated into something that a browser can work with. Listing 5-2 shows the HTML sent to the browser before the validator controls are fired (with the listing reformatted to make it easier to read). Listing 5-2 The HTML source that the browser sees when Login.aspx is requested and before the validator controls are fired
Draw Data Matrix ECC200 In VB.NET
Using Barcode generation for VS .NET Control to generate, create Data Matrix image in .NET framework applications.
www.OnBarcode.com
Painting PDF 417 In C#
Using Barcode generator for VS .NET Control to generate, create PDF417 image in Visual Studio .NET applications.
www.OnBarcode.com
<html> <body> <form name="_ctl0" method="post" action="login.aspx" language="javascript" onsubmit="ValidatorOnSubmit();" id="_ctl0"> <input type="hidden" name="__VIEWSTATE" value="dDwxMDgxMzYzOTAxOzs+" />
GTIN - 12 Encoder In C#
Using Barcode printer for .NET Control to generate, create UPC Symbol image in .NET applications.
www.OnBarcode.com
Bar Code Creator In Visual C#
Using Barcode maker for .NET Control to generate, create bar code image in .NET framework applications.
www.OnBarcode.com
<script language="javascript" src="/aspnet_client/system_web/1_0_3217_0/WebUIValidation.js"> </script> <center> <h3> <font face="Verdana" color=blue>Login Page</font>
EAN / UCC - 14 Generation In C#.NET
Using Barcode maker for .NET framework Control to generate, create USS-128 image in .NET framework applications.
www.OnBarcode.com
Creating Code 93 Extended In Visual C#
Using Barcode drawer for Visual Studio .NET Control to generate, create ANSI/AIM Code 93 image in Visual Studio .NET applications.
www.OnBarcode.com
</h3> <table> <tr> <td> Email: </td> <td> <input name="UserEmail" id="UserEmail" type="text" size="30" /> </td> <td> <span id="_ctl1" controltovalidate="UserEmail" errormessage="*" evaluationfunction= "RequiredFieldValidatorEvaluateIsValid" initialvalue="" style="color:Red;visibility:hidden;">*</span> </td> </tr> <tr> <td> Password: </td> <td> <input name="UserPass" id="UserPass" type="password" size="30" /> </td>
Barcode Reader In C#
Using Barcode Control SDK for .NET framework Control to generate, create, read, scan barcode image in .NET framework applications.
www.OnBarcode.com
Bar Code Generator In None
Using Barcode creation for Software Control to generate, create barcode image in Software applications.
www.OnBarcode.com
<td> <span id="_ctl2" controltovalidate="UserPass" errormessage="*" evaluationfunction= "RequiredFieldValidatorEvaluateIsValid" initialvalue="" style="color:Red;visibility:hidden;">*</span> </td> </tr> <tr> <td colspan=3 align="center"> <input type="submit" name="_ctl3" value="Login" onclick="if (typeof(Page_ClientValidate) == function ) Page_ClientValidate(); " language="javascript" /> <p> <span id="Msg" style="color:Red;font -family:Verdana;font-size:10pt;"> </span> </td> </tr> </table> </center>
PDF 417 Printer In None
Using Barcode creator for Office Excel Control to generate, create PDF417 image in Excel applications.
www.OnBarcode.com
Scan Barcode In Visual C#.NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
<script language="javascript"> <! var Page_Validators = new Array(document.all["_ctl1"], document.all["_ctl2"]);
Code 3/9 Scanner In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Scanning UPC - 13 In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
// > </script>
Barcode Reader In Java
Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
www.OnBarcode.com
Generate QR Code 2d Barcode In None
Using Barcode generator for Software Control to generate, create QR Code JIS X 0510 image in Software applications.
www.OnBarcode.com
<script language="javascript"> <! var Page_ValidationActive = false; if (typeof(clientInformation) != "undefined" && clientInformation.appName.indexOf("Explorer") != -1) { if (typeof(Page_ValidationVer) == "undefined") alert("Unable to find script library " + " /aspnet_client/system_web/1_0_3217_0" + "/WebUIValidation.js . " + "Try placing this file manually, " + "or reinstall by running aspnet_regiis -c ."); else if (Page_ValidationVer != "125") alert("This page uses an incorrect " + "version of WebUIValidation.js. The page expects " + "version 125. The script library is " + Page_ValidationVer + "."); else ValidatorOnLoad(); }
function ValidatorOnSubmit() { if (Page_ValidationActive) { ValidatorCommonOnSubmit(); } }
// > </script>
</form> </body> </html>
Note
Listing 5-2 contain s one unusual field: a hidden field named __VIE WSTAT E. This field is used to maintai n the state of controls from submis sion to submis sion. You should leave this field alone. It s designe d so that it can t be modifie d, to prevent you from, for exampl e, hijackin g another
user s state informa tion. For more informa tion about this topic, see the sidebar ASP.N ET Forms vs. Visual Basic 6.0 Forms, earlier in this chapter . Wow. That s a lot of code! Not surprisingly, the C# script block at the top of Listing 5-1 isn t present in Listing 5-2, since it is identified as a runat=server script block. However, there is a new <SCRIPT> block, as follows: <script language="javascript" src="/aspnet_client/system_web/1_0_3217_0/WebUIValidation.js"> </script> This code, of course, doesn t look even a little familiar! Looking at the Internet Information Services console, we can see that, sure enough, a directory is logically located where the src attribute on the <SCRIPT> tag points to, as shown in Figure 5-5.
Figure 5-5 : The Internet Information Services console, showing the ASP.NET client code directory I won t show the entire WebUIValidation.js file because the version you ll be using will probably differ slightly from the version I m using. The significant point here is that a script library designed for the client side is used in your pages when you re doing clientside validation. Note You can also set the location of the client-side scripts in the webControls tag in the Machine.config file. Generally, it s best to
leave this location setting alone, but the ability to have the location of these client-side JavaScript files might be useful. The next major change in the resulting HTML is the <SPAN> tag that seems to replace the first RequiredFieldValidator element, as follows: <span id="_ctl1" controltovalidate="UserEmail" errormessage="*" evaluationfunction= "RequiredFieldValidatorEvaluateIsValid" initialvalue="" style="color:Red;visibility:hidden;">*</span> An HTML <SPAN> tag is used to provide a container for a section of text that might require special rendering. In this case, the special rendering is that the text shouldn t be visible. This is accomplished using a standard style attribute. The validator control associated with the Password text box is similarly changed to a <SPAN> tag. The <ASP:Button> element from Listing 5-1 is transformed into a traditional HTML submit button, as shown here: <input type="submit" name="_ctl3" value="Login" onclick="if (typeof(Page_ClientValidate) == function ) Page_ClientValidate(); " language="javascript" /> As you can see, the page validation we requested is now handled by client-side JavaScript. But how exactly A script tag below the form actually sets a variable named Page_Validators that contains an element for each of the validator controls. The onclick event of the submit button calls Page_ClientValidate. The version of Page_ClientValidate present in my version of WebUIValidation.js looks like this: function Page_ClientValidate() { var i; for (i = 0; i < Page_Validators.length; i++) { ValidatorValidate(Page_Validators[i]); } ValidatorUpdateIsValid(); ValidationSummaryOnSubmit(); Page_BlockSubmit = !Page_IsValid; return Page_IsValid; } Each element of the Page_Validators array is individually validated by calling ValidatorValidate, another function in WebUIValidation.js. This function calls the function specified in the evaluationFunction attribute of the <SPAN> tag for each of the validator controls. The details of how this all works aren t critical, but you should be aware of where the processing is taking place. The CompareValidator Control Referring back to Login.aspx, in Listing 5-1, you ll see that logic is used to compare the e-mail address and password entered to hard-coded values. If the e-mail and password don t match what s expected, another label on the form will be set to a message requesting the user to try again. We haven t seen that message yet, because when we clicked the Login button, the client-side validators fired and displayed those red asterisks before the form was submitted. Because the client-side validators didn t indicate that the page was valid, a round-trip to the server wasn t necessary.
ASP.NET includes a CompareValidator control to compare two values. Using a CompareValidator control could be useful for creating, for example, a password change page, on which the new password must be entered correctly twice to ensure that the password is set to the value the user intended. Suppose that we wanted to use the CompareValidator control rather than the Login_Click server-side logic. For the password, we could change the RequiredFieldValidator control to a CompareValidator control, as follows: <asp:CompareValidator id="comp1" ControlToValidate="UserPass" ValueToCompare = "password" Type="String" runat="server"/> The ValueToCompare attribute is one way to specify what is to be compared in a CompareValidator control, but another possible way is to use the CompareToControl attribute. Set this attribute to the ID of another control on the form, and the CompareValidator control will instead compare the value of the ControlToValidate attribute to the value of the control pointed to by CompareToControl. If you use the ValueToCompare attribute, an unfortunate side effect can occur. For example, if you use the previous CompareValidator code, the following code would replace the CompareValidator code and be returned to the client: <span id="comp1" controltovalidate="UserPass" evaluationfunction="CompareValidatorEvaluateIsValid" valuetocompare="password" style="color:Red;visibility:hidden;"></span> This is almost certainly not what you would want to do. In the generated HTML returned to the client browser, the <SPAN> tag contains, in clear text, the ValueToCompare attribute. This example is obviously contrived, but in the real world, you ll certainly encounter situations in which you d prefer not to expose so much to the client. One solution is to change the clienttarget attribute of the Page directive. Listing 5-1 didn t have a Page directive, but you could add the following line: <%@ Page Language="c#" clienttarget=downlevel %> When this directive is added to the Login.aspx code shown in Listing 5-1, rather than the HTML code shown in Listing 5-2, the browser sees the code shown in Listing 5-3. Listing 5-3 The HTML sent to the browser when Login.aspx in Listing 5-1 has the clienttarget=downlevel attribute added to the Page directive
Copyright © OnBarcode.com . All rights reserved.