What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 21801 |  Welcome, Guest!   Register  Login
Home > Tutorials > ASP.NET AJAX Tutorials > Timer Control

ASP.NET Tutorials | Report a Bug in this Tutorial
Found interesting? Add this to:


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



About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 9:03:11 PM