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")

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was hoping that ASP.NET provided a public API to do this, but overwriting the JS functions will work.
0

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!

Comments

0

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,

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.