惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
小众软件
小众软件
Y
Y Combinator Blog
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LangChain Blog
Martin Fowler
Martin Fowler
美团技术团队
B
Blog RSS Feed
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
博客园 - 司徒正美
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
Hugging Face - Blog
Hugging Face - Blog
D
Docker
G
Google Developers Blog

博客园 - .............

验证码生成器 十年编程无师自通 模仿桌面外观 Web服务是否失去前进目标 “开源”SOA正在改写IT规划方程式 C#实现的18位身份证格式验证算法 - ............. - 博客园 怎样使用Junit Framework进行单元测试的编写 从一个项目谈XP在国内的应用 - ............. - 博客园 AOP是什么? 在C#中使用Conditional元数据attribute来实现Debug代码 - ............. - 博客园 IBM CRM 系统解决方案 HR软件之我见-小议目前HR供应商 Atlas学习手记(11):使用ModalPopup Extender Atlas学习手记(10):使用AlwaysVisibleControl Extender Atlas学习手记(9):异步调用Page Method Atlas学习手记(8):调用本地Web Service简单介绍 Atlas学习手记(7):使用DragOverlayExtender实现拖放功能 Atlas学习手记(6):使用Atlas UpdateProgress控件 Atlas学习手记(4):使用AutoComplete Extender实现自动完成功能 Atlas学习手记(3):由UpdatePanel开始
Atlas学习手记(5):使用服务端定时控件TimerControl
............. · 2006-08-03 · via 博客园 - .............

转自 TerryLee技术空间
摘要:TimerControl是一个用于服务器端定时器的控件,可用来实时显示数据等,在很多地方都有应用,本文将简单介绍一下TimerControl的使用。

主要内容

1TimerControl介绍

2.完整示例

一.TimerControl介绍

TimerControl是一个用于服务器端定时器的控件,可用来实时显示数据等,在很多地方都有应用,本文将简单介绍一下TimerControl的使用。一个简单的TimerControl如下:

<atlas:TimerControl runat="server" Interval="3000" ID="tickerTimer" OnTick="tickerTimer_Tick" />

它的属性解释如下:

属性

解释

Interval

时间间隔,隔多长时间刷新一次,单位为ms

Interval="3000"

OnTick

每隔Interval时间后向服务器端触发事件,是一个服务器端的方法

OnTick="tickerTimer_Tick"

Enabled

设置TimerControl控件是否可用,通过此属性我们可以自行控制开启和停止定时。

二.完整示例

下面我们通过一个简单的示例来演示TimerControl的使用。在很多网站上我们都可以看到一些股票代码等信息,这些数据都是实时刷新的,这里我们模仿一个股票代码示例。

1.添加ScriptManager,这个不用多说,只要是Atlas应用都必须添加的。设置它的EnablePartialRendering属性为true,这里要用UpdatePanel来做局部刷新。

<atlas:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server" />

2.添加TimerControl控件

<atlas:TimerControl runat="server" Interval="3000" ID="tickerTimer" OnTick="tickerTimer_Tick" />

代码很简单,指定间隔的时间为3s,触发的事件为tickerTimer_Tick

3.添加UpdatePanel,用两个Label来分别显示公司的名称和虚拟股票代码:

<atlas:UpdatePanel runat="server" ID="UpdatePanel1">

    
<Triggers>

        
<atlas:ControlEventTrigger ControlID="tickerTimer" EventName="Tick" />

    
</Triggers>

    
<ContentTemplate>

      
<h2>Atlas TimerControl Example</h2>

      
<asp:Label ID="CompanyName" runat="server" Font-Bold="True" Font-Size="Larger">Tokyo Traders:</asp:Label>

      
<asp:Label ID="CompanyValue" runat="server" Font-Bold="True" Font-Size="Larger" ForeColor="Red">20</asp:Label>

    
</ContentTemplate>

</atlas:UpdatePanel>

4.编写一个简单的Web Service,用来返回股票代码,这里我们用产生一个随机数来模拟:

using System;

using System.Web;

using System.Collections;

using System.Web.Services;

using System.Web.Services.Protocols;
 

/// <summary>

/// Summary description for TimerWebService

/// </summary>


[WebService(Namespace 
= "http://tempuri.org/")]

[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]

public class TimerWebService : System.Web.Services.WebService {

    
public TimerWebService () {

        
//Uncomment the following line if using designed components 

        
//InitializeComponent(); 

    }


    [WebMethod]

    
public string GetCode()

    
{
        Random r1 
= new Random();

        
return r1.Next(20,200).ToString();

    }

}

5.编写TimerControl的触发事件tickerTimer_Tick,代码很简单,只要把返回的数据显示在Label上就可以了。

protected void tickerTimer_Tick(object sender, EventArgs e)

{
    TimerWebService service 
= new TimerWebService();

    
this.CompanyValue.Text = service.GetCode();

}

至此一个简单的TimerControl示例就完成了,看一下运行效果,起始的时候:


3s
之后:

完整示例下载:http://terrylee.cnblogs.com/Files/Terrylee/TimerControlDemo.rar