文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>[ASP.NET AJAX]How to register javascript functions after UpdatePanel updated

[ASP.NET AJAX]How to register javascript functions after UpdatePanel updated

时间:2010-10-18  来源:Jerry Weng

01 <form id="form1" runat="server">
02 <asp:ScriptManager ID="ScriptManager1" runat="server">
03 </asp:ScriptManager>
04
05 <script type="text/javascript">
06 function pageLoad(sender, e) {
07 alert("Page is re-loaded!");
08 }
09 </script>
10
11 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
12 <ContentTemplate>
13 <%=DateTime.Now.ToString() %>
14 </ContentTemplate>
15 <Triggers>
16 <asp:AsyncPostBackTrigger ControlID="TriggerTimer" EventName="Tick" />
17 </Triggers>
18 </asp:UpdatePanel>
19
20 <asp:Timer ID="TriggerTimer" runat="server" Interval="5000">
21 </asp:Timer>
22 </form>



In this snippet, we could see we register the pageLoad function directly in the page. the function pageLoad will be executed when the page first load and each asynchronous postback which caused by UpdatePanel.

Method B:

The code above in pageLoad is same as the code below.

 

1 <script type="text/javascript">
2 window.onload = function () {
3 Sys.Application.add_load(function () {
4 alert("Page re-loaded!");
5 });
6 }
7 </script>

 

Server Side:

For server side, we need to use a method, ScriptManager.RegisterClientScriptBlock, for more details about this method, please view this link in MSDN:http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerclientscriptblock.aspx. This method registers a client script block with the ScriptManager control for use with a control that is inside an UpdatePanel control, and then adds the script block to the page.

For example,

 

1 protected void TriggerTimer_Tick(object sender, EventArgs e)
2 {
3 ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "RegScript",
4 "alert('Page re-loaded!');", true);
5 }

The result is same as the methods which talked above.

排行榜 更多 +
探聊交友

探聊交友

聊天通讯 下载
天骄通

天骄通

旅游住宿 下载
奥的斯

奥的斯

商务办公 下载