5

I'm trying to run javascript once an update panel has refreshed. I've read a few places that you can use code similar to this:

function codeToRun() {
    //Code Here
}

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(codeToRun);

But it doesn't seem to be working... any ideas? I've tried putting the code inside the Content area of the update panel as well as outside of it... haven't had any luck so far. Please let me know if you have any insight to why this might be happening.

Thanks,
Matt

1
  • 2
    Reason #1827 to switch to MVC. Good question though, as I'm unfortunately still stuck in the quagmire of webforms. Commented Nov 14, 2011 at 17:56

2 Answers 2

8

Everything needs to be outside of the UpdatePanel:

Markup:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

Script:

if( typeof(Sys) != "undefined" )
{
    Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(initRequestHandler);     
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);   
}
else
{
    //we have a problem with ScriptManager
}
Sign up to request clarification or add additional context in comments.

1 Comment

As we're using 'System.Web.UI.ScriptManager.RegisterStartupScript()' on the code-behind, instead of ScriptManager tags, I simply omitted the ScriptManager tags and used the add_endRequest() portion above, and it worked. Cool!
0

The call to System.WebForms.PageRequestManager should come after the library has been included and initialized, but probably not inside the UpdatePanel.

2 Comments

How do you include and initialize it?
@Matt it is included when you add the ScriptManager and UpdatePanels to the page - they won't work without them, so that's an easy way to verify they are there :). They're usually near the top of the <body> tag.

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.