Search
Author
ASP.NET Tutorials
Author
Sheo Narayan
Winners

Win Prizes

Social Presence
Like us on Facebook

Silverlight Tutorials | Report a Bug in the Tutorial

Timer Control

Timer control performs postbacks at the defined intervals. This is generally used to update the content of UpdatePanel at a defined interval. Apart from this it can be used to post the whole page at defined interval.

There are few properties that are used more frequetly, these are

Interval Gets or sets the time interval in milliseconds after OnTick event of Timer control will fire.
OnTick Used to specifiy the method name that will after specified time interval.


Timer control can be used in two ways.

DEMO : Timer Control Show Source Code
1. Inside an UpdatePanel control
When the timer control is used inside an UpdatePanel control, the Timer control automatically works as a trigger for
the UpdatePanel control, however we can override this behavior by setting the ChildrenAsTriggers property of UpdatePanel as false.

In this case lets say server takes 7 seconds to process the request then following message
will change after every 12 (5+7) seconds as I have specified Timer interval property as 5000 milliseconds (5 seconds).

Following message will change after every 5 seconds

Please wait Timer Control from inside UpdatePanel will fire in 5 seconds
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label runat="Server" ID="lblMessage1" Text="Please wait ..." SkinID="FormValueMessage" />
        // Timer control inside UpdatePanel
        <asp:Timer runat="Server" ID="Timer1" OnTick="Timer1_Tick" Interval="5000" />
    </ContentTemplate>    
</asp:UpdatePanel>
Download Sample project with Source Code
2. Timer Control Outside UpdatePanel
When the Timer control is outside an UpdatePanel control, we must explicitely define the Timer control as trigger
for the UpdatePanel control to be updated.

In this case Timer, Timer OnTick event will fire after every 3 seconds irrespective of
how many seconds server takes to process the request as I have specified Interval as 3000 milliseconds.

Following message will change after every 3 seconds

Please wait Timer Control from outside UpdatePanel will fire in 3 seconds
<asp:UpdatePanel runat="server" ID="UpdatePanel2" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer2" EventName="Tick" />
    </Triggers>
    <ContentTemplate>
        <asp:Label runat="Server" ID="lblMessage2" SkinID="FormValueMessage" />
    </ContentTemplate>    
</asp:UpdatePanel>


// Timer control outside UpdatePanel
<asp:Timer runat="Server" ID="Timer2" OnTick="Timer2_Tick" Interval="3000" />
Download Sample project with Source Code