Timed refresh : Refresh « Ajax « ASP.NET Tutorial






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

<!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">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <div>
                    <asp:Label ID="Label1" runat="server" Font-Bold="True"></asp:Label>
                    <br />
                    This time refreshes automatically every 1 second (for about 10 seconds).<br />
                    <br />
                    <asp:Button ID="Button1" runat="server" Text="Refresh Time" />
                </div>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="TimerControl1" EventName="Tick" />
            </Triggers>
            
        </asp:UpdatePanel>
        <asp:Timer ID="TimerControl1" runat="server" Interval="1000" OnTick="TimerControl1_Tick">
        </asp:Timer>
        
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class TimedRefresh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = DateTime.Now.ToLongTimeString();
    }

    
    protected void TimerControl1_Tick(object sender, EventArgs e)
    {
        int tickCount = 0;
        if (ViewState["TickCount"] != null)
        {
            tickCount = (int)ViewState["TickCount"];
        }
        tickCount++;
        ViewState["TickCount"] = tickCount;
        if (tickCount > 10)
        {
            TimerControl1.Enabled = false;
        }

    }
}








20.9.Refresh
20.9.1.Timed refresh