19.6. Validating Data with Code
Validation can be complex in a business application where the rules can change frequently. You may need to read data from several controls on the page, query a database, or contact a Web service to determine if the user's input is valid. For example, after a customer enters a postal code, you can check whether the closest warehouse has sufficient quantity to ship the product by the chosen date.
The CustomValidator calls client-side and server-side functions that you provide — no built-in freebies in this validator! In this example, you test the users input and simulate a server-side routine that checks the available quantity. Instead of creating a separate client-side validation, I show you how the ASP.NET AJAX UpdatePanel control validates without a full page refresh.
Follow these steps to implement a CustomValidator control:
From the AJAX Extensions category of the Toolbox, add a ScriptManager control and an UpdatePanel control to the ASP.NET page.
Inside the UpdatePanel control, drop a TextBox control and a Button control onto the form.
Drop a CustomValidator control on the page.
In the CustomValidator control's Properties window, set the ControlToValidate property to TextBox1 (or whatever you named your control) and the Display value to Dynamic.
In Design view, double-click the Button control to create a handler routine for its Click event and add the following code as the handler:
Protected Sub Button1_Click _ (ByVal sender As Object, _ ByVal e ...