David Ly
David Ly

Reputation: 31586

How do you get client-side script to execute on an ASP.NET postback? (from an UpdatePanel)

Basically I want to get some javascript to be sent back to the client and executed from a postback inside an UpdatePanel. Furthermore this is inside of a reusable WebControl.

I've tried this.Page.ClientScript.RegisterStartupScript and this.Page.ClientScript.RegisterClientScriptBlock but Firebug shows that those scripts aren't sent back in the postback's response.

I also tried straight up writing a <script> tag inside the control's main div in the Render method, which does get sent back but isn't executed.

I got a hacky solution working, but ideally looking for a cleaner solution. The hack is to add a 1x1 pixel img with width/height set to 0 and use that for an onload event to execute the script and that works, but it seems like there has to be a cleaner way to do this.

Upvotes: 0

Views: 762

Answers (1)

Aristos
Aristos

Reputation: 66641

If I understand correct you look for the endRequest event - to avoid the onload of your image. With this code you get the event of your panel update on javascript side.

var prm = Sys.WebForms.PageRequestManager.getInstance();    
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);

function InitializeRequest(sender, args) {      
}

function EndRequest(sender, args) {
}

Upvotes: 3

Related Questions