Thursday, July 2, 2009

Auto Page refresh Sample Using C#, Ajax

Auto Page refresh Sample Using  C#, Ajax
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="autoreferesh.aspx.cs" Inherits="autoreferesh" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
     <asp:Label ID="Label2" runat="server" Text="This is Time, When The Full Page Load :" Font-Bold="true"></asp:Label>&nbsp;
     <asp:Label ID="MyLabel" runat="server"></asp:Label><br /><br />       
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            <asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="30000">
            </asp:Timer>
        </div>
        <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
            </Triggers>
            <ContentTemplate>
                <asp:Label ID="Label3" runat="server" Text="This is The Time when Page will Referesh :" Font-Bold="true"></asp:Label>&nbsp;
                <asp:Label ID="Label1" runat="server" Text="Page not refreshed yet."></asp:Label><br />
                <asp:Label ID="Label4" runat="server" Text="(Page Will Referesh after Every 30 Sec)" Font-Bold="true"></asp:Label>&nbsp;
                <br /><br />
            </ContentTemplate>
        </asp:UpdatePanel>     
    </form>
</body>
</html>

code behind

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 autoreferesh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MyLabel.Text = System.DateTime.Now.ToString();
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {

        Label1.Text = "Page Refreshed at: " + DateTime.Now.ToLongTimeString();

    }


}

No comments: