在多个UpdatePanle中使用Timer控件

翻译|其它|编辑:郝浩|2007-10-25 11:05:40.000|阅读 887 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

本文将使用 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 属性为10000。Interval 属性的单位是毫秒,所以我们设置为10000,相当于10秒钟刷新一次。
11.双击 Timer 控件添加 Tick 事件处理,在事件处理中设置 Label1和 Label2的 Text 属性值,代码如下。在 UpdatePanel1和 UpdatePanel2中添加 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.等待10秒钟后两个 UpdatePanel 都刷新后,Label 中的文本都变成了当前时间。

标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:csdn

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP