views:

781

answers:

3

Is there a way to run some custom Javascript whenever a client-side ASP.NET validator (RequiredFieldValidator, RangeValidator, etc) is triggered?

Basically, I have a complicated layout that requires I run a custom script whenever a DOM element is shown or hidden. I'm looking for a way to automatically run this script when a validator is displayed. (I'm using validators with Display="dynamic")

A: 

The best solution I've identified for my specific situation is this:

  1. Create a global JS data structure mapping control IDs to a visibility state.
  2. Register the client IDs of the validators (or anything else, for that matter) in this data structure.
  3. Every 250 milliseconds, loop through the global data structure and compare the cached visiblity state with the element's current state. If the states are different, update the cache and run the custom resize script.

This is ugly in lots of ways, and it's only a solution for my specific scenario, not the abstract case where we want to piggyback arbitrary code onto the showing/hiding of a validator. I'd love a better suggestion!

Seth Petry-Johnson
A: 

i'm not sure if got your question right but here goes... you can add a custom validator (or maybe handle the onblur event), in your javascript custom validation, you can call Page_ClientValidate() and check for Page_IsValid for errors. Something like the code below:

function customValidation()
{
    Page_ClientValidate();
    if(!Page_IsValid)
    { //run your resize script }
}

HTH,

Leon Tayson
That's certainly better than the hack I'm currently using, but I was really looking for a way to tap into the validation cycle at a global level, rather than handling each validator individually.
Seth Petry-Johnson
+1  A: 

See this comment for how I managed to extend the ASP.Net client side validation. Others have managed to extend it using server side techniques.

Geoff
I was hoping that ASP.NET provided a public API to do this, but overwriting the JS functions will work.
Seth Petry-Johnson