Home > Ajax, Asp.net > Refresh Content in UpdatePanel using Asp.net Ajax Timer Control

Refresh Content in UpdatePanel using Asp.net Ajax Timer Control


Lets say we have a requirement where we have a label control and we have assigned date time to its text, and in every 5 seconds we want to update the time, over there we need to place the label control inside the update panel and use a timer control to update its content.

<%@ 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 runat="server">
    <title>Update Panel and Timer</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:Timer runat="server" id="UpdateTimer" interval="5000"
          ontick="UpdateTimer_Tick" />
        <asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
            <Triggers>
                <asp:AsyncPostBackTrigger controlid="UpdateTimer"
                  eventname="Tick" />
            </Triggers>
            <ContentTemplate>
                <asp:Label runat="server" id="lbldatetime" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
</body>
</html>

In the codebehind:

protected void UpdateTimer_Tick(object sender, EventArgs e)
{  
lbldatetime.Text = DateTime.Now.ToString();
}
Advertisements
  1. Frank
    July 31, 2013 at 9:32 am

    thanks man it helped a lot

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: