问题
Possible Duplicate:
how to call a javascript fn in Update panel on Partial postback in asp.net
update panel is doing partial post back, and I have a Timer_Tick method where I call the method to do data binding and updatepanel onLoad for some calculation for graph. I use a Javascript to draw the graph
window.onload = function () {
r.init();
};
SO the graph is displayed when the page loads, when the update panel updates after 4 seconds the r.init is not called. I tried many things. the graph disappears. can someone give me a example of how to execute the JavaScript after the page partial postback.
<asp:UpdatePanel runat="server" ID="Holder" OnLoad="Graphstats" UpdateMode="Always" ChildrenAsTriggers="True">
<ContentTemplate>
...
<asp:Timer ID="Timer1" runat="server" Interval="3000" OnTick="Timer_Tick" />
</ContentTemplate>
</asp:UpdatePanel>
回答1:
I believe that within Timer_Tick, you need to set it up to call that script again when it's rendered using RegisterStartupScript.
string script = "<script type=text/javascript>r.init();</";
script+= "script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "GraphInit", script);
ClientScriptManager.RegisterStartupScript
回答2:
Your site has an asp:ScriptManager. This control communicates with the PageRequestManager object in Javascript. You can hook into all communications of updatepanels with your code.
- beginRequest
- endRequest
- initializeRequest
- pageLoaded
- pageLoading
I think pageLoaded is the best for your approach.
<script type="text/javascript">
window.onload = function () {
// PageLoad for sync only
r.init();
};
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function(sender, args) {
// PageLoad for sync and async!
r.init();
});
</script>
You can extend the code to check which control caused the post in order to optimize your code.
MSDN: Sys.WebForms.PageRequestManager
来源:https://stackoverflow.com/questions/7637682/asp-net-page-life-cycle-error