在多个UpdatePanle中使用Timer控件 (四)

本文介绍如何在ASP.NET页面中使用Timer控件配合多个UpdatePanel实现局部刷新功能。通过设置UpdatePanel的UpdateMode属性为Conditional,并将Timer控件作为AsyncPostBackTrigger,实现了每隔10秒自动更新两个UpdatePanel中的Label内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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属性值,代码如下。

 1None.gifpublic partial class _Default : System.Web.UI.Page
 2None.gif
 3ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 4InBlock.gif
 5InBlock.gif    protected void Page_Load(object sender, EventArgs e)
 6InBlock.gif
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif
 9ExpandedSubBlockEnd.gif    }

10InBlock.gif
11InBlock.gif    protected void Timer1_Tick(object sender, EventArgs e)
12InBlock.gif
13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
14InBlock.gif
15InBlock.gif        Label1.Text = "UpdatePanel1 refreshed at: " +
16InBlock.gif
17InBlock.gif          DateTime.Now.ToLongTimeString();
18InBlock.gif
19InBlock.gif        Label2.Text = "UpdatePanel2 refreshed at: " +
20InBlock.gif
21InBlock.gif          DateTime.Now.ToLongTimeString();
22InBlock.gif
23ExpandedSubBlockEnd.gif    }

24InBlock.gif
25ExpandedBlockEnd.gif}

12.在UpdatePanel1UpdatePanel2中添加Timer控件作为AsyncPostBackTrigger,代码如下:

None.gif<Triggers>
None.gif
None.gif  
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
None.gif
None.gif
</Triggers>

全部完成后ASPX页面代码如下:

 1ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2None.gif
 3None.gif 
 4None.gif
 5None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 6None.gif
 7None.gif<html xmlns="http://www.w3.org/1999/xhtml">
 8None.gif
 9None.gif<head id="Head1" runat="server">
10None.gif
11None.gif    <title>Untitled Page</title>
12None.gif
13None.gif</head>
14None.gif
15None.gif<body>
16None.gif
17None.gif    <form id="form1" runat="server">
18None.gif
19None.gif        <asp:ScriptManager ID="ScriptManager1" runat="server" />
20None.gif
21None.gif        <div>
22None.gif
23None.gif            <asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="10000">
24None.gif
25None.gif            </asp:Timer>
26None.gif
27None.gif        </div>
28None.gif
29None.gif        <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
30None.gif
31None.gif            <Triggers>
32None.gif
33None.gif                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
34None.gif
35None.gif            </Triggers>
36None.gif
37None.gif            <ContentTemplate>
38None.gif
39None.gif                <asp:Label ID="Label1" runat="server" Text="UpdatePanel1 not refreshed yet."></asp:Label>
40None.gif
41None.gif            </ContentTemplate>
42None.gif
43None.gif        </asp:UpdatePanel>
44None.gif
45None.gif        <asp:UpdatePanel ID="UpdatePanel2" UpdateMode="Conditional" runat="server">
46None.gif
47None.gif            <Triggers>
48None.gif
49None.gif                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
50None.gif
51None.gif            </Triggers>
52None.gif
53None.gif            <ContentTemplate>
54None.gif
55None.gif                <asp:Label ID="Label2" runat="server" Text="UpdatePanel2 not refreshed yet."></asp:Label>
56None.gif
57None.gif            </ContentTemplate>
58None.gif
59None.gif        </asp:UpdatePanel>
60None.gif
61None.gif 
62None.gif
63None.gif    </form>
64None.gif
65None.gif</body>
66None.gif
67None.gif</html>

13.保存并按Ctrl + F5运行。

14.等待10秒钟后两个UpdatePanel都刷新后,Label中的文本都变成了当前时间。

 

转自:http://www.cnblogs.com/Terrylee/archive/2006/11/15/Using_Timer_Control_with_More_Than_One_UpdatePanel_Control.html




 

转载于:https://www.cnblogs.com/chjw8016/archive/2007/08/11/851828.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值