13.07.2015 Views

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9: Validation Controls 237Ask the ExpertQ: Won’t a button function fire first if I put it first in the C# code before I put inthe validation function?A: It does not matter in which order your C# methods are placed as far as a buttonfunction or validation function is concerned. The validation function uses thesecond parameter (ServerValidateEventArgs e) to establish validity.The ServerValidateEventArgs parameter has an IsValid property thatyou can set to true or false. (The default is true.) Once your validation function hasdetermined whether the validation is successful, it launches the event associatedwith the button event handler. In this way, the button event can include the Page.IsValid statement and set up a response for the user to see. The Page.IsValidstatement uses the value set with ServerValidateEventArgs instance,e.IsValid.Try This Customizing ValidationThe <strong>ASP</strong>.<strong>NET</strong> side of the application includes a single TextBox control, a CustomValidatorcontrol, a button and two labels used for directions, and validation feedback. The followingsteps show how to develop the application:1. Open a new Web Site in Visual Basic 2008, and set the view to Split mode.2. Drag a TextBox control to the page in the Design window.3. Place a CustomValidator control beneath the TextBox. In the Source mode add anOnServerValidate property, and assign it the value “CityCheck”.4. Add a button beneath the validator. Change the text to “Find City”, and double-click thebutton to generate an event handler for the OnClick property. Change the OnClickvalue to “DoValidation”.5. Place two Label controls beneath the button. Use Figures 9-11 and 9-12 as guides.Remove the Text property from both labels in the Source window of the Split mode.6. Select the CustomValidator control and in the Properties window, change the errormessage to “City not recognized. Try again.” Set ControlToValidate to“TextBox1”. The following listing shows all of the source code settings for the<strong>ASP</strong>.<strong>NET</strong> portion of the application:(continued)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!