The CustomValidator Control

CustomValidator Control"-->

only for RuBoard

The CustomValidator Control

The CustomValidator control enables you to create your own validation function in both client and server code. The server code you write can be developed in your default language (for example: C#.NET), but, for your client script, you must write in a scripting language that your target browser supports. Today's most widely used client-scripting languages are JavaScript and its competing Microsoft version, JScript. If you're developing strictly for Internet Explorer, you can write in VBScript.

We'll focus primarily on two things. The first is the ServerValidate event. You need to handle this event to perform server-side validation using the CustomValidator control. This event expects two parameters: Object , and ServerValidateEventArgs . ServerValidateEventArgs contains a string of the value that needs to be validated accessed through the ServerValidate EventArgs.Value property. You use the Boolean ServerValidateEventArgs.IsValid attribute to store the result of your validation routine. You use OnServerValidate to wire the event handler to the CustomValidator control.

The second thing I want to discuss is the ClientValidationFunction property, which is what you'll use to either GET or SET the name of a client-side script that will be used to validate the control. This method should have two parameters, source and value , with value being the characters you want to validate. This function should return a Boolean value.

Listing 9.8 demonstrates how to use the CustomValidator and how to implement both server- and client-side validation. The validation in this example merely checks that the amount of characters entered into a TextBox doesn't exceed 100 characters.

Listing 9.8 Using the CustomValidation Control
 [VisualBasic.NET] 01: <script language="vb" runat="server"> 02: 03:  public sub txtDescription_Validate(sender as Object, args as ServerValidateEventArgs) 04: 05:   if (args.Value.Length > 100) then 06: 07:    args.IsValid = false 08: 09:   else 10: 11:    args.IsValid = true 12: 13:   end if 14: 15:  end sub 16: 17: </script> [C#.NET] 01: <script language="C#" runat="server"> 02: 03:  void txtDescription_Validate(Object sender, ServerValidateEventArgs args){ 04: 05:   if (args.Value.Length > 100) { 06: 07:    args.IsValid = false; 08: 09:   }  else { 10: 11:    args.IsValid = true; 12: 13:   } 14: 15:  } 16: 17: </script> [VisualBasic.NET & C#.NET] 18: <html> 19:  <head> 20:   <script language="JavaScript"> 21: 22:    function txtDescription_ClientValidate(source, value){ 23: 24:     if(value.Value.length > 100) { 25: 26:      value.IsValid = false; 27: 28:     }  else { 29: 30:      value.IsValid = true; 31: 32:     } 33: 34:    } 35: 36:   </script> 37:  </head> 38:  <body Style="font-size:10"> 39:   <form runat="server"> 40: 41:     <h3>CustomValidator</h3> 42: 43:     <asp:TextBox 44:      id="txtDescription" 45:      runat="server" 46:      TextMode="MultiLine" 47:      Rows="10" 48:     /> 49: 50:     <p> 51: 52:     <asp:CustomValidator 53:      runat="server" 54:      ControlToValidate="txtDescription" 55:      OnServerValidate="txtDescription_Validate" 56:      ClientValidationFunction="txtDescription_ClientValidate" 57:      ErrorMessage="Text must be 100 characters or less" 58:      Display="Dynamic" 59:     /> 60: 61:     <p> 62: 63:     <asp:Button 64:      runat="server" 65:      Text="Submit" 66:     /> 67: 68:   </form> 69:  </body> 70: </html> 

Listing 9.8 contains a multi-line TextBox which is wired up with the CustomValidator control (lines 52 “59). Within the CustomValidator control, I have OnServerValidate set to txtDescription_Validate ”the ServerValidate event handler. ClientValidationFunction is set to the client-side function txtDescription_ClientValidate . Within both the server-side event handler and the client-side function, I have the same code, which checks the Length property of the string passed in. If the string is greater than 100, then the control is found to be invalid. Figure 9.5 contains an illustration of the page after more than 100 characters were entered into the TextBox.

Figure 9.5. The control is invalid because more than 100 characters were entered into the TextBox .
graphics/09fig05.gif

Now enter less than 100 characters and try submitting the page again ”the page is valid. As you can see, there are many posiblilities available when using the CustomValidator control.

only for RuBoard


Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170

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