Disabling ValidationTypically, a form includes a Cancel button that enables you to stop working on the form and navigate to a new page. Implementing a Cancel button in a form that includes validation controls, however, is more difficult than you might expect. The problem is that the client-side validation scripts can prevent any subroutine associated with the Cancel button from ever executing. To get around this problem, you need to use a special property of the Button , LinkButton , and ImageButton controls named the CausesValidation property. You can use the CausesValidation property to enable or disable validation when a particular button is clicked. For example, the page in Listing 3.21 contains both a Submit and Cancel button. The CausesValidation property is assigned the value False in the case of the Cancel button. Listing 3.21 CausesValidation.a spx
<Script runat="Server">
Sub btnSubmit_Click( s As Object, e As EventArgs )
If IsValid Then
Response.Redirect( "ThankYou.aspx" )
End If
End Sub
Sub btnCancel_Click( s As Object, e As EventArgs )
Response.Redirect( "Cancel.aspx" )
End Sub
</Script>
<html>
<head><title>CausesValidation.aspx</title></head>
<body>
<form runat="Server">
Enter your first name:
<br>
<asp:TextBox
id="txtFirstName"
Runat="Server" />
<asp:RequiredFieldValidator
ControlToValidate="txtFirstName"
Text="Required!"
Runat="Server" />
<p>
<asp:Button
id="btnSubmit"
Text="Submit"
OnClick="btnSubmit_Click"
Runat="Server" />
<asp:Button
id="btnCancel"
Text="Cancel"
OnClick="btnCancel_Click"
CausesValidation="False"
Runat="Server" />
</form>
</body>
</html>
The C# version of this code can be found on the CD-ROM.p |
SummaryThis chapter covered all the ASP.NET validation controls. You learned how to make required form fields by using the RequiredFieldValidator control, how to perform complex pattern validation by using the RegularExpressionValidator control, how to compare values and data types by using the CompareValidator control, and how to check for a range of values by using the RangeValidator control. You also learned how to summarize validation errors by using the ValidationSummary control. You learned how to summarize errors within the page itself and how to display the summary in a pop-up dialog box. Finally, you learned how to implement custom server-side and client-side validation functions by using the CustomValidator control. You should now be ready to handle any conceivable Web site validation task. |
Chapter 4. Advanced Control ProgrammingIN THIS CHAPTER
In this chapter, you examine several advanced
First, you delve deeper into the details of view state. You learn how to enable and disable view state for particular controls on a page, and how to disable view state for a whole page. You also learn how to store your own
Next, you learn how to gain programmatic access to the controls in your pages and set control properties to hide and display controls. You also learn how to programmatically add new controls to a page. This skill enables you to create pages with complex interactions between the form elements.
NOTE You can view "live" versions of many of the code samples in this chapter by visiting the Superexpert Web site at: http://www.Superexpert.com/AspNetUnleashed/ Finally, you examine several of the rich controls included with ASP.NET. You learn how to use the Calendar control to create interactive calendars, use the AdRotator control to display rotating banner advertisements, and use the HTMLInputFile control to accept file uploads at your Web site. |