Click here to Skip to main content
13,899,460 members
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 2 Dec 2005
Licenced CPOL

Auto postback in ASP.NET

, 20 Jul 2009
Rate this:
Please Sign up or sign in to vote.
How to do auto postback in ASP.NET.


After updating this article on 06 July 2009, I see that there is someone who thinks that I don’t know that there are other solutions to this task today. As Stephen Inglish pointed out in a message, one of them is to use the AJAX timer module, and as fare as I know, there may be other solutions to.

Back in 2005, when I first wrote this article using Visual Studio 2003, I was not able to find a solution for automatic postback at a regular time schedule. After searching for a time, without finding anything, I wrote this JavaScript and shared it with the CodeProject community.

After the article was published in 2005, there have been some comments from isikiss and obed21, members of the community, which have improved the code. The reason why I’m updating this article, even though it is outdated, is that there are still people who read this article and I think they deserve an updated code.

Using the code

Start a new ASP.NET Web Application. Call it whatever you want, maybe TimerTest, and do the following:

In Design mode, click on the Source button and paste this JavaScript inside the head tag.

<script language="JavaScript" type="text/javascript">
// Script to generate an automatic postBack to the server
var secs
var timerID = null
var timerRunning = false
var delay = 1000
function InitializeTimer()
    // Set the length of the timer,
     // in seconds. Your choise
    secs = 5

function StopTheClock()
    timerRunning = false
function StartTheTimer()
    if (secs==0)

        //Generate a Postback to the server
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)

Your Body have to look something like this. The most important thing is to add onload="InitializeTimer()" inside the <body> tag.

<body onload="InitializeTimer()">
<form id="form1" runat="server">
       The time is:   <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

In the code-behind, you can paste in this just to see something happening.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
          Handles Me.Load

  'This is just to se something happening
  Label1.Text = Now.ToString("hh:mm:ss")

  'Put the code to execute here.
End Sub

Leave it like this. The only thing it’s doing is that it posts back to the server, and in this example, it’s refreshing the time every 5 seconds.

And that’s all.


  • 06 July 2009: The article was made when I was using Visual studio 2003. There have also been some comments by isikiss and obed21, members of the community, that have improved the code. I have implemented the improvements they have suggested and used Visual Studio 2008 to test it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sigurd Johansen
Web Developer
Norway Norway
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionThank you Pin
Hany Shebl24-Jun-14 3:35
memberHany Shebl24-Jun-14 3:35 
Generalexactly what i needed Pin
weirdlNet26-Oct-09 23:57
memberweirdlNet26-Oct-09 23:57 
GeneralMy vote of 1 Pin
Rogic21-Jul-09 1:10
memberRogic21-Jul-09 1:10 
GeneralRe: My vote of 1 Pin
Sigurd Johansen21-Jul-09 6:30
memberSigurd Johansen21-Jul-09 6:30 
GeneralQuery Pin
lovedotnet20-Jul-09 19:02
memberlovedotnet20-Jul-09 19:02 
GeneralRe: Query Pin
Sigurd Johansen21-Jul-09 6:28
memberSigurd Johansen21-Jul-09 6:28 
GeneralMy vote of 1 Pin
ahmed-itani14-Jul-09 1:13
professionalahmed-itani14-Jul-09 1:13 
GeneralRe: My vote of 1 Pin
Sigurd Johansen18-Jul-09 10:36
memberSigurd Johansen18-Jul-09 10:36 
GeneralYes .. really outdated Pin
Abhishek Sur13-Jul-09 21:21
professionalAbhishek Sur13-Jul-09 21:21 
General[My vote of 2] Outdated Pin
Stephen Inglish7-Jul-09 13:19
memberStephen Inglish7-Jul-09 13:19 
GeneralRe: [My vote of 2] Outdated Pin
VaSig7-Jul-09 21:58
memberVaSig7-Jul-09 21:58 
GeneralRe: [My vote of 2] Outdated Pin
Stephen Inglish17-Jul-09 7:36
memberStephen Inglish17-Jul-09 7:36 
GeneralRe: [My vote of 2] Outdated [modified] Pin
Sigurd Johansen17-Jul-09 8:44
memberSigurd Johansen17-Jul-09 8:44 
GeneralRe: [My vote of 2] Outdated Pin
Md. Marufuzzaman20-Jul-09 21:20
mentorMd. Marufuzzaman20-Jul-09 21:20 
Generalyour article has helped me so much! Pin
791671157-Jul-09 12:15
member791671157-Jul-09 12:15 
GeneralNot Posting back Pin
Agweet5-Aug-08 3:13
memberAgweet5-Aug-08 3:13 
GeneralRe: Not Posting back Pin
Sigurd Johansen10-Aug-08 8:15
memberSigurd Johansen10-Aug-08 8:15 
GeneralFix to the code Pin
obed2113-Aug-07 6:48
memberobed2113-Aug-07 6:48 
Questionhow to sending sms using vb script or c# Pin
malake27-Nov-06 20:58
membermalake27-Nov-06 20:58 
GeneralSolution to increasing refresh rate Pin
isikiss7-Aug-06 11:57
memberisikiss7-Aug-06 11:57 
GeneralRe: Solution to increasing refresh rate Pin
Sigurd Johansen7-Nov-06 10:26
memberSigurd Johansen7-Nov-06 10:26 
GeneralGood Pin
venkata krishnan K S12-Feb-06 23:23
membervenkata krishnan K S12-Feb-06 23:23 
Generalaccess denied Pin
paulains31-Dec-05 8:55
memberpaulains31-Dec-05 8:55 
GeneralAnother easy alternative Pin
Peter Lanoie8-Dec-05 5:53
memberPeter Lanoie8-Dec-05 5:53 
GeneralRe: Another easy alternative Pin
Umer Khan16-May-07 21:46
memberUmer Khan16-May-07 21:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 20 Jul 2009
Article Copyright 2005 by Sigurd Johansen
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid