Tutorial :Run javascript function after Server-Side validation is complete



Question:

Ok, I've got a lightbox with a small form (2 fields) in it, inside an UpdatePanel, and I want to close this lightbox (must be done via javascript) when the 'Save' button is pressed.

However, there is a need to have a server-side CustomValidator on the page, and I only want to close the lightbox if this returns as valid.

Does anyone know a way to trigger javascript (or jQuery) code from a server-side validator?


Solution:1

You can add a little snippet of code using the ScriptManager to execute after the response comes back to the UpdatePanel.

if (Page.IsValid){       ScriptManager.RegisterStartupScript(              customValidator1,               typeof(MyPageClass),               "closeBox",              "myLightBoxVariableOnThePage.close()",              true);  }  


Solution:2

When that server side validator runs, it will send a whole new page to the browser. Anything that was shown in the browser before was destroyed, including any state kept in your javascript. If new page bears a strong resemblance to the old page, you should consider this a happy coincidence.

Therefore, the thing to do here is rather than executing a javascript function, have your CustomValidator make the correct changes to the page on success so that it's rendered to the browser correctly in the first place.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »