【正文】
tements with Visual Basic. Furthermore, unlike VBScript, Visual Basic is a piled language. This means that if you use Visual Basic to rewrite the same code that you wrote with VBScript, you can get better performance. If you have worked only with VBScript and not Visual Basic in the past, don39。 they are the formatting properties of the base control class. Next, you learn how to apply Cascading Style Sheet styles and classes to Web controls. 3. Performing Form Validation with Validation Controls Using Clientside Validation Traditionally, Web developers have faced a tough choice when adding form validation logic to their pages. You can add form validation routines to your serverside code, or you can add the validation routines to your clientside code. The advantage of writing validation logic in clientside code is that you can provide instant feedback to your users. For example, if a user neglects to enter a value in a required form field, you can instantly display an error message without requiring a roundtrip back to the server. People really like clientside validation. It looks great and creates a better overall user experience. The problem, however, is that it does not work with all XVI browsers. Not all browsers support JavaScript, and different versions of browsers support different versions of JavaScript, so clientside validation is never guaranteed to work. For this reason, in the past, many developers decided to add all their form validation logic exclusively to serverside code. Because serverside code functions correctly with any browser, this course of action was safer. Fortunately, the Validation controls discussed in this chapter do not force you to make this difficult choice. The Validation controls automatically generate both clientside and serverside code. If a browser is capable of supporting JavaScript, clientside validation scripts are automatically sent to the browser. If a browser is incapable of supporting JavaScript, the validation routines are automatically implemented in serverside code. You should be warned, however, that clientside validation works only with Microsoft Inter Explorer version and higher. In particular, the clientside scripts discussed in this chapter do not work with any version of Netscape Navigator. Requiring Fields: The RequiredFieldValidator Control You use RequiredFieldValidator in a Web form to check whether a control has a value. Typically, you use this control with a TextBox control. However, nothing is wrong with using RequiredFieldValidator with other input controls such as RadioButtonList. Validating Expressions: The RegularExpressionValidator Control You can use RegularExpressionValidator to match the value entered into a form field to a regular expression. You can use this control to check whether a user has entered, for example, a valid address, telephone number, or username or password. Samples of how to use a regular expression to perform all these validation tasks are provided in the following sections. Comparing Values: The CompareValidator Control The CompareValidator control performs parisons between the data entered into a form field and another value. The other value can be a fixed value, such as a particular number, or a value entered into another control. Summarizing Errors: The ValidationSummary Control XVII Imagine that you have a form with 50 form fields. If you use only the Validation controls discussed in the previous sections of this chapter to display errors, seeing an error message on the page might be difficult. For example, you might have to scroll down to the 48th form field to find the error message. Fortunately, Microsoft includes a ValidationSummary control with the Validation controls. You can use this control to summarize all the errors at the top of a page, or wherever else you want. 4. Advanced Control Programming Working with View State By default, almost all controls retain the values of their properties between form posts. For example, if you assign text to a Label control and submit the form, when the page is rendered again, the contents of the Label control are preserved. The magic of view state is that it does not depend on any special server or browser properties. In particular, it does not depend on cookies, session variables, or application variables. View state is implemented with a hidden form field called VIEWSTATE that is automatically created in every Web Forms Page. When used wisely, view state can have a dramatic and positive effect on the performance of your Web site. For example, if you display database data in a control that has view state enabled, you do not have to return to the database each time the page is posted back to the server. You can automatically preserve the data within the page39。 the control is not processed for either prerendering or rendering. Web controls (but not every HTML control) have an additional property named Enabled. When Enabled has the value False and you are using Inter Explorer version or higher, the control appears ghosted and no longer functions. When used with other browsers, such as Netscape Navigator, the control might not appear ghosted, but it does not function. Disabling View State In certain circumstances, you might want to disable view state for an individual control or for an page as a whole. For example, you might have a control that contains a lot of data (imagine a RadioButtonList control with 1,000 options). You might not want to load the data into the hidden __VIEWSTATE form field if you are worried that the form data would significantly slow down the rendering of the page. Using Rich Controls In the following sections, you learn how to use three of the more featurerich controls in the framework. You learn how to use the Calendar con