用ASP.Net AJAX开发Web程序 -- Timer、UpdateProgress篇


Timer控制顾名思意,它的作用是在固定的时间内刷新UpdatePanel,使其异步更新。它的使用和Windows Form中的Timer作用类似。而UpdateProgress的作用是,当异步更新时服务器处理时间较长时,UpdateProgress控件可以生成提示信息,提示使用者程序正在运行。无疑有了这两个控件,我们写的程序将更加友好。下面我们各用两个示例来说明这两个控件的使用。


一、   使用Timer控件
1.Timer的重要属性/方法
 
属性/方法 说明
Interval 该属性设置多少时间内自动刷新,单位为毫秒。
Tick 当自动刷新时间到了以后,触发该事件。

 
2.      使用Timer控件
 
下面是一段使用Timer控件的简单示例。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Timer.aspx.cs" Inherits="Timer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script runat="server">
        void Timer1_Tick(object sender, EventArgs e)
        {
            string temp;
            temp = Label2.Text;
            if (temp == "2")
                temp = "3";
            else if (temp == "3")
                temp = "2";
            else
                temp = "2";
            Label2.Text = temp;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        &nbsp;</div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
                </asp:Timer>
                <asp:Label ID="Label1" runat="server" Text="Label"><%=DateTime.Now.ToString() %></asp:Label>
                <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
            </ContentTemplate>
        </asp:UpdatePanel>
        &nbsp;&nbsp;
    </form>
</body>
</html>


在上面的代码里面,我们实现了一个异步更新的服务器端时间的效果,Interval设置为1000,表示每一秒种更新一次。并且触发了Tick事件,该事件更改了Label2的Text的属性。通过这个示例,很容易就了解Timer的使用方法了。
 
2.       Timer控件使用总结
 
Timer在程序需要处理定时局部更新的时候,就可以派上用场了。注意Timer会触发所有UpdatePanel的UpdateMode设置为Always的更新

二、使用UpdateProgress控件

1. UpdateProgress重要属性/方法
 
属性/方法 说明
DisplayAfter 该属性设置多少时间后才出现提示信息,单位为毫秒,默认为500毫秒。
DynamicLayout 设定UpdateProgress是否占位,默认值为True。
Visible UpdateProgress信息是否可见,默认值为True。
associatedUpdatePanelID 指定UpdateProgress绑定的UpdatePanel的ID。

 
2.      使用UpdateProgress控件
 
使用下面这个示例来简单的学习UpdateProgress的使用。我们使用上面讲到的Timer来触发一个异步更新,在触发事件Tick中让线程停止3秒,这样我们就可以看到UpdateProgress中的提示信息”Process…”了。下面是源代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpdateProgress.aspx.cs" Inherits="UpdateProgress" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script runat="server">
        void Timer1_Tick(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(3000);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
   
    </div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                &nbsp;<asp:Timer ID="Timer1" runat="server" Interval="2000" OnTick="Timer1_Tick">
                </asp:Timer>
            </ContentTemplate>
        </asp:UpdatePanel>
        &nbsp; &nbsp;
        <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
            <ProgressTemplate>
                Process...
            </ProgressTemplate>
        </asp:UpdateProgress>
    </form>
</body>
</html>


假如上面的程序中,UpdateProgress的属性AssociateUpdatePanelID没有指定为UpdatePanel1是不会出现“Process…”的,我们可以通过这个属性来处理多个UpdatePanel的等待信息。如果DisplayAfter的值设置为大于3秒也是不会出现“Process…”的。
 
3.      UpdateProgress控件使用总结
 
UpdateProgress的存在让我们的异步更新程序的界面更加友好。我们可以通过各种方式提示用户系统正在处理信息,与此同时,用户可以进行其它的操作,而不用在那边无聊的等待

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3