Skip to Content
ASP.NET 3.5 For Dummies®
book

ASP.NET 3.5 For Dummies®

by Ken Cox
February 2008
Intermediate to advanced
428 pages
8h 52m
English
For Dummies
Content preview from ASP.NET 3.5 For Dummies®

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:

  1. From the AJAX Extensions category of the Toolbox, add a ScriptManager control and an UpdatePanel control to the ASP.NET page.

  2. Inside the UpdatePanel control, drop a TextBox control and a Button control onto the form.

  3. Drop a CustomValidator control on the page.

  4. 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.

  5. 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

ASP.NET 3.5 Unleashed

ASP.NET 3.5 Unleashed

Stephen Walther
ASP.NET 4 Unleashed

ASP.NET 4 Unleashed

Stephen Walther, Kevin Hoffman, Nate Dudek

Publisher Resources

ISBN: 9780470195925Purchase book