Ed James
Ed James

Reputation: 10607

Run javascript function after Server-Side validation is complete

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?

Upvotes: 0

Views: 2215

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 415690

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.

Upvotes: 0

womp
womp

Reputation: 116977

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);
}

Upvotes: 2

Related Questions