Validating Text Entry The StringValidator Example


Validating Text Entry ”The StringValidator Example

In this section, we will create a new validator named StringValidator by deriving from the BaseValidator class. StringValidator is designed to validate string values entered in a text box. This validator contains the functionality of a RequiredFieldValidator and ensures that the user enters a string that differs from the text box's initial value. A common requirement of Web applications is to limit the amount of text entered by the user. Therefore, StringValidator will also validate that the user's input does not exceed a specified number of characters .

As with the existing validator controls in ASP.NET, StringValidator always performs server-side validation. The control performs client-side validation when the browser requesting the page supports JavaScript. Client-side validation allows the StringValidator to indicate errors before the page is submitted to the server. This prevents a wasteful round-trip and alerts the user to errors more quickly.

Figure 14-1 illustrates a page containing a StringValidator associated with a TextBox control in an error state. The page also contains an instance of a ValidationSummary control that summarizes the errors on the page.

Figure 14-1. The sample page containing the StringValidator in its invalid state

graphics/f14hn01.jpg

Listing 14-1 contains the implementation of the sample page shown in Figure 14-1.

Listing 14-1 StringValidatorTest.aspx
 <%@Pagelanguage="c#"%> <%@RegisterTagPrefix="msp"Assembly="MSPress.ServerControls" Namespace="MSPress.ServerControls"%> <html> <scriptrunat="server"> protectedvoidButton1_Click(objectsender,EventArgse){ if(Page.IsValid){ Label1.Text="Youentered:"+TextBox1.Text; } } </script> <head> <title>StringValidatorSample</title> </head> <body> <formmethod="post"runat="server"> <p> Enteravalue: <br/> <asp:TextBoxid="TextBox1"runat="server"Columns="80"/> <msp:StringValidatorid="StringValidator1"runat="server" ControlToValidate="TextBox1" MaximumLength="80"IsRequired="true" ErrorMessage="Enteravalueinthetextbox.Itmustbeless than80characters." Text="Enteravalidvalue."/> <br/> <asp:Buttonid="Button1"runat="server"Text="Submit" OnClick="Button1_Click"/> <br/> <asp:Labelid="Label1"runat="server"/> <br/> <asp:ValidationSummaryrunat="server"HeaderText="Errors:"/> </p> </form> </body> </html> 

The sample page illustrates the simplicity of incorporating validation into a Web application. The validator control is associated with the TextBox by using its ControlToValidate property. The code in the page simply checks the IsValid property of the Page before processing any user input. The validation framework in ASP.NET implements the plumbing needed to allow validators to validate user entries on the client and on the server.

Listing 14-2 shows portions of the HTML that were rendered by the TextBox , the StringValidator (distinguished by the boldface font), and the ASP.NET validation framework.

Listing 14-2 The HTML generated from StringValidatorTest.aspx
 <html> ... <body> <formmethod="post" action="StringValidatorTest.aspx"language="javascript" onsubmit="ValidatorOnSubmit();"> ... <scriptlanguage="javascript" src="/aspnet_client/system_web/1_0_3705_0/WebUIValidation.js"> </script>  <scriptlanguage="JavaScript"src="/aspnet_client/mspress_servercon trols/1_0_0_0/StringValidator.js">  </script> ... <p> Enteravalue: <br/> <inputname="TextBox1"type="text"size="80"id="TextBox1"/>  <spanid="StringValidator1"controltovalidate="TextBox1" errormessage="Enteravalueinthetextbox.Itmustbeless than80characters." evaluationfunction="StringValidatorEvaluateIsValid" initialValue=""isRequired="True"maxLength="80" style="color:Red;visibility:hidden;"> Enteravalidvalue. </span>  <br/> <inputtype="submit"name="Button1"value="Submit" onclick="if(typeof(Page_ClientValidate)=='function') Page_ClientValidate();" language="javascript"id="Button1"/> <br/> <divid="_ctl0"headertext="Errors:" style="color:Red;display:none;"></div> </p> ... <scriptlanguage="javascript"> <!-- varPage_ValidationSummaries=newArray(document.all["_ctl0"]); varPage_Validators=newArray(document.all["StringValidator1"]); //--> </script> <scriptlanguage="javascript"> 
 <!-- varPage_ValidationActive=false; if(typeof(clientInformation)!="undefined"&& clientInformation.appName.indexOf("Explorer")!=-1){ if(typeof(Page_ValidationVer)=="undefined") alert("Unabletofindscriptlibrary'/aspnet_client/system_web /1_0_3705_0/WebUIValidation.js'.Tryplacingthisfilemanually,orrei nstallbyrunning'aspnet_regiis-c'."); elseif(Page_ValidationVer!="125") alert("ThispageusesanincorrectversionofWebUIValidation.j s.Thepageexpectsversion125.Thescriptlibraryis"+ Page_ValidationVer+"."); else ValidatorOnLoad(); } functionValidatorOnSubmit(){ if(Page_ValidationActive){ ValidatorCommonOnSubmit(); } } //--> </script> </form> </body> </html> 

Listing 14-3 contains the implementation of the StringValidator control.

Listing 14-3 StringValidator.cs
 usingSystem; usingSystem.Collections; usingSystem.ComponentModel; usingSystem.Configuration; usingSystem.Globalization; usingSystem.Reflection; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; namespaceMSPress.ServerControls{ [ ToolboxData("<{0}:StringValidatorrunat=\"server\""+ "ErrorMessage=\"StringValidator\""+ "Text=\"*\"></{0}:StringValidator>") ] publicclassStringValidator:BaseValidator{ 
 [ Bindable(true), Category("Behavior"), DefaultValue(""), Description("Theinitialvaluefromwhichthenewvalue"+ "mustbedifferent") ] publicstringInitialValue{ get{ strings=(string)ViewState["InitialValue"]; return(s==null)?String.Empty:s; } set{ ViewState["InitialValue"]=value; } } [ Category("Behavior"), DefaultValue(true), Description("Whetheravalueisrequired.") ] publicvirtualboolIsRequired{ get{ objectb=ViewState["IsRequired"]; return(b==null)?true:(bool)b; } set{ ViewState["IsRequired"]=value; } } [ Bindable(true), Category("Behavior"), DefaultValue(0), Description("Themaximumlengthofavalidvalue") ] publicvirtualintMaximumLength{ get{ objecti=ViewState["MaximumLength"]; return(i==null)?0:(int)i; } set{ ViewState["MaximumLength"]=value; } } 
 protectedoverridevoidAddAttributesToRender(HtmlTextWriterwriter){ base.AddAttributesToRender(writer); if(RenderUplevel){ writer.AddAttribute("evaluationfunction", "StringValidatorEvaluateIsValid"); writer.AddAttribute("initialValue", InitialValue.Trim()); writer.AddAttribute("isRequired", IsRequired.ToString(CultureInfo.InvariantCulture)); writer.AddAttribute("maxLength", MaximumLength.ToString(CultureInfo.InvariantCulture)); } } privatestringGetClientIncludes(){ stringclientFilesUrlPrefix=null; //Usetheconfigsettingtodeterminewheretheclient //filesarelocated.Clientfilesarelocatedinthe //aspnet_clientv-rootandthendistributedinto //subfoldersbyassemblynameandassemblyversion. if(Context!=null){ IDictionaryconfigData= (IDictionary)Context.GetConfig("system.web/webControls"); if(configData!=null){ clientFilesUrlPrefix= (string)configData["clientScriptsLocation"]; } } if((clientFilesUrlPrefix!=null)&& (clientFilesUrlPrefix.IndexOf("{0}")>=0)){ AssemblyNameassemblyName= this.GetType().Assembly.GetName(); stringassembly= assemblyName.Name.Replace('.','_').ToLower(); stringversion= assemblyName.Version.ToString().Replace('.','_'); clientFilesUrlPrefix= String.Format(clientFilesUrlPrefix,assembly, 
 version); } returnString.Format("<scriptlanguage=\"JavaScript\"src=\"{0}\"></script>", clientFilesUrlPrefix+"StringValidator.js"); } protectedoverrideboolEvaluateIsValid(){ //Getthevaluetobevalidated. stringcontrolValue= GetControlValidationValue(ControlToValidate); if(controlValue==null){ //Impliesthevalidatorisnotboundtoacontrol. returntrue; } controlValue=controlValue.Trim(); if(IsRequired&& (controlValue.Equals(InitialValue.Trim())==true)){ returnfalse; } intmaxLength=MaximumLength; if((maxLength!=0)&&(controlValue.Length>maxLength)){ returnfalse; } returntrue; } protectedoverridevoidOnPreRender(EventArgse){ base.OnPreRender(e); if(RenderUplevel){ Page.RegisterClientScriptBlock(typeof(StringValidator).FullName, GetClientIncludes()); } } } } 

StringValidator derives from the abstract BaseValidator class and automatically inherits the functionality needed to participate in the validation framework. BaseValidator implements the IValidator interface and the logic required to register itself with the Validators collection of its Page . This ensures that the validator is invoked when the page attempts to determine its valid state. Base ­Validator is responsible for emitting the reference to the WebUIValidation.js script file and page script (shown in Listing 14-2) that implements the client-side validation framework. BaseValidator also implements the ControlToValidate property, which allows the page developer to associate validators with corresponding input controls. Finally, BaseValidator renders the user interface (UI) of a validator when the validator is in an invalid state. Thus, the BaseValidator class performs all the plumbing necessary to fit into the validation framework and allows you to focus on the validation logic that is specific to your validator.

StringValidator implements a number of properties that are specific to its validation functionality. The IsRequired property is used to turn on the validation logic that ensures that the user makes an entry in the associated TextBox . The InitialValue property is used to indicate the initial value of the TextBox that must be changed when the IsRequired property is set to true . The MaximumLength property is used to turn on the validation logic that ensures that any entry made by the user in the associated TextBox does not exceed the specified length.

StringValidator implements the abstract EvaluateIsValid method of Base ­Validator to implement its own validation logic based on the page developer's choices. This method is called by the server-side validation framework when the page needs to determine its valid state. StringValidator returns true if and only if all the validity checks are completed successfully. StringValidator uses the GetControlValidationValue method of BaseValidator to retrieve the value that needs to be validated. This helper method encapsulates the logic to find the associated validation target based on the ControlToValidate property and to access the value of the property specified via the ValidationProperty ­Attribute metadata on the associated control class.

The StringValidator class is marked with the ToolboxDataAttribute metadata attribute. This metadata attribute, which we described in Chapter 5, "Developing a Simple Custom Control," is used to specify the fragment of HTML that is used to instantiate the control when it is first dropped onto the design surface in a design-time environment. The ToolboxDataAttribute metadata attribute can be used to specify default values for the ErrorMessage and Text properties, which are required for any validator to be visible to the user.

StringValidator implements its client-side validation functionality in StringValidator.js. This file is deployed as a client file, which StringValidator refers to in the page by generating a <script src=""> tag. StringValidator overrides the OnPreRender method of BaseValidator to emit the reference to its associated script file. StringValidator uses the value of the RenderUplevel property of BaseValidator to determine whether its client-side validation should be enabled. BaseValidator contains all the logic needed to determine when to enable client-side validation. We described in detail how to package client-side behavior into script files and to enable client-side behavior in Chapter 13.

StringValidator.js contains a method that implements the same validation logic as the EvaluateIsValid method of the StringValidator class. This method is invoked by the client-side validation framework before the page is submitted. If the method returns false , the submission process is interrupted . The functionality in StringValidator.js uses the utility functions present in WebUIValidation.js, such as ValidatorTrim and ValidatorGetValue . WebUIValidation.js implements the ASP.NET client-side validation framework. This client-side validation framework contains a number of other utility functions, including enabling and disabling validators dynamically and converting validation values.

Listing 14-4 shows StringValidator.js.

Listing 14-4 StringValidator.js
 functionStringValidatorEvaluateIsValid(val){ varvalueToValidate= ValidatorTrim(ValidatorGetValue(val.controltovalidate)); if(val.isRequired=='True'){ if(valueToValidate==val.initialValue){ returnfalse; } } if(val.maxLength!='0'){ if(valueToValidate.length>parseInt(val.maxLength,10)){ returnfalse; } } returntrue; } 


Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

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