Asp.net page life cycle error, [duplicate]

你说的曾经没有我的故事 提交于 2019-12-12 01:23:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!