ASP.NET AJAX入門系列(11):在多個UpdatePanle中使用Timer控件

本文將使用Timer控件更新兩個UpdatePanel控件,Timer控件將放在UpdatePanel控件的外面,並將它配置爲UpdatePanel的觸發器,翻譯自官方文檔。

 

主要內容

    在多個UpdatePanel中使用Timer控件

 

1.添加一個新頁面並切換到設計視圖。

2.如果頁面沒有包含ScriptManager控件,在工具箱中的AJAX Extensions標籤下雙擊ScriptManager控件添加到頁面中。

3.雙擊Timer控件添加到Web頁面中。Timer控件可以作爲UpdatePanel的觸發器不管它是否在UpdatePanel中。

4.雙擊UpdatePanel控件添加一個Panel到頁面中,並設置它的UpdateMode屬性值爲Conditional

5.再次雙擊UpdatePanel控件添加第二個Panel到頁面中,並設置它的UpdateMode屬性值爲Conditional

6.在UpdatePanel1中單擊,並在工具箱中Standard標籤下雙擊Label控件添加到UpdatePanel1中。

7.設置Label控件的Text屬性值爲“UpdatePanel1 not refreshed yet”。

8.添加Label控件到UpdatePanel2

9.設置第二個Label控件的Text屬性值爲“UpdatePanel2 not refreshed yet”。


10
.設置Interval屬性爲10000Interval屬性的單位是毫秒,所以我們設置爲10000,相當於10秒鐘刷新一次。

11.雙擊Timer控件添加Tick事件處理,在事件處理中設置Label1Label2Text屬性值,代碼如下。.在UpdatePanel1UpdatePanel2中添加Timer控件作爲AsyncPostBackTrigger,代碼如下:

public partial class _Default : System.Web.UI.Page

{

    
protected void Page_Load(object sender, EventArgs e)

    
{

    }


    
protected void Timer1_Tick(object sender, EventArgs e)

    
{

        Label1.Text 
= "UpdatePanel1 refreshed at: " +

          DateTime.Now.ToLongTimeString();

        Label2.Text 
= "UpdatePanel2 refreshed at: " +

          DateTime.Now.ToLongTimeString();

    }


}

12

<Triggers>

  
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />

</Triggers>

全部完成後ASPX頁面代碼如下:

.保存並按Ctrl + F5運行。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    
<title>Untitled Page</title>

</head>

<body>

    
<form id="form1" runat="server">

        
<asp:ScriptManager ID="ScriptManager1" runat="server" />

        
<div>

            
<asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="10000">

            
</asp:Timer>

        
</div>

        
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">

            
<Triggers>

                
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />

            
</Triggers>

            
<ContentTemplate>

                
<asp:Label ID="Label1" runat="server" Text="UpdatePanel1 not refreshed yet."></asp:Label>

            
</ContentTemplate>

        
</asp:UpdatePanel>

        
<asp:UpdatePanel ID="UpdatePanel2" UpdateMode="Conditional" runat="server">

            
<Triggers>

                
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />

            
</Triggers>

            
<ContentTemplate>

                
<asp:Label ID="Label2" runat="server" Text="UpdatePanel2 not refreshed yet."></asp:Label>

            
</ContentTemplate>

        
</asp:UpdatePanel>

 

    
</form>

</body>

</html>


13

14.等待10秒鐘後兩個UpdatePanel都刷新後,Label中的文本都變成了當前時間。

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章