dating for seniors home page - Validating checkboxlist asp net control using javascript

The Pre Render event is raised just before the control is rendered on the page, so it the best moment to inject some client side functionality into the page.

validating checkboxlist asp net control using javascript-86

css styles applied to non-valid fields, validation summary direct links to specified invalid fields, and so on and so forth), dead easy disable on submit buttons, unparalleled support, and so much more I'm not remembering to mention right now.

You can implement many of the above yourself (while much other Peter Blum functionality remains time/effort/skill prohibitive to duplicate), but using a fraction of the time and effort you can implement with all the nice finishing touches included as a bonus. Agree, if you need to perform a lot of stuff, then using a 3rd party control library is good, but I only needed that one, and it took me less than half a day to look for a solution on Google, adapt it for my needs, test it, and write a blog post about it, so less then the cost of the single webserver license.

And then I adapted the same thing also for list of Ratings controls inside the ASP.

If you add a normal Required Field Validator and you want to validate a Check Box List, you will get a runtime exception informing you that the Check Box List cannot be validated.

Sometimes you want to make sure the that user has selected at least one of the checkbox in the checkbox list, but as aforementioned, if you user a normal Required Field Validator you will get an exception.

You could use a Custom Validator control, but a nicer and more reusable approach is to build a custom web control that extends the Base Validator.

I found a 2001 article on how to do that (Building a Check Box List Validator Control) but it doesn't work properly on ASP. So I have to make some small changes to make it work on ASP. First thing you have create a class that inherits from Base Validator In this we check if the control is null and if the control can be cast to a Check Box List.

This is the methods that performs the server side validation in case Java Script is not enabled on the client, or not supported.

In this method I just loop through all the items of the Check Box List and if I find one that is selected then I return true.

Now we have the validation working on the server after a postback, but it would be nice if we could perform the validation directly on the client when the user toggles the state of the checkboxes.

To accomplish this there is another step to take: inject some client side Java Script in the On Pre Render method.

Tags: , ,