How to control which JavaScript runs after a partial postback of UpdatePanel endRequest?

I know that I can connect to client-side events to trigger JavaScript after each partial postback; however I want to do something like this:

protected void FooClicked(object sender, EventArgs e) { ClientScript.RegisterStartupScript(GetType(), "msg", "showMsg('Foo clicked');",true); } 

I know that I could hack it completely with hidden fields and start something after each postback, but there should be a pretty direct way to do it differently.

+6
javascript asp.net-ajax updatepanel
source share
4 answers

The specific code example that you describe does not work with partial post-feedbacks, because ClientScript.RegisterStartupScript() writes JS to the page during the build phase of the request life cycle output; whereas partial postback only updates the selected part of the page using JavaScript (even if markup is created on the server for the entire page, including your script run).

In order to accurately match what you are describing, you must include the Literal control inside your UpdatePanel, and during the partial postback, set the Text property in the content pane to the script you want to run:

 myLiteral.Text = "<script type=\"JavaScript\">doStuff();</script>"; 

IMO, the more correct way is to use the client API for asynchronous postbacks to register an event handler that will execute after the postback

 function endRequestHandler(sender, args) { doStuff(); } Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler); 

If you need to pass information that was created during the postback to the handler, you can pass it through hidden fields and capture it from the DOM in your client-side handler.

+9
source share

This is much more elegant, IMO. obviously, ASP AJAX will call any JS function called pageLoad. so write a function called pageLoad and it will execute every time the page loads, even after a partial refresh.

+2
source share

 <script type="text/javascript" language="javascript"> function ValidateUserRole() { var answer = confirm("Are you sure..................") if (answer) { var btnRoleReleatedButton = '<%=btnRoleReleatedButton.ClientID%>'; document.getElementById(btnRoleReleatedButton).click(); } else { return false; } } </script> 

 Tier 3 :<asp:UpdatePanel ID="upTier3Details" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:DropDownList ID="ddlTier3" runat="server" CssClass="ddlextralarge" Width="350" /> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="ddlUserRole" EventName="SelectedIndexChanged" /> </Triggers> </asp:UpdatePanel> 

+1
source share

the best way to do this is to use the DoAfterPostBack control. you can download this control for free here http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html

0
source share

All Articles