Click here to Skip to main content
Licence CPOL
First Posted 2 Dec 2005
Views 128,254
Bookmarked 45 times

Auto postback in ASP.NET

By | 20 Jul 2009 | Article
How to do auto postback in ASP.NET.

Introduction

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

    StopTheClock()
    StartTheTimer()
}
function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}
function StartTheTimer()
{
    if (secs==0)
    {
        StopTheClock()

        //Generate a Postback to the server
        document.forms[0].submit()  
     
    }
    else
    {
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}
//-->
</script>

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">
 <div>
       The time is:   <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>

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.

History

  • 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.

License

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

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalexactly what i needed PinmemberweirdlNet23:57 26 Oct '09  
GeneralMy vote of 1 PinmemberRogic1:10 21 Jul '09  
GeneralRe: My vote of 1 PinmemberSigurd Johansen6:30 21 Jul '09  
GeneralQuery Pinmemberlovedotnet19:02 20 Jul '09  
GeneralRe: Query PinmemberSigurd Johansen6:28 21 Jul '09  
GeneralMy vote of 1 PinmemberKronass1:13 14 Jul '09  
GeneralRe: My vote of 1 PinmemberSigurd Johansen10:36 18 Jul '09  
GeneralYes .. really outdated PinmemberAbhishek Sur21:21 13 Jul '09  
General[My vote of 2] Outdated PinmemberStephen Inglish13:19 7 Jul '09  
GeneralRe: [My vote of 2] Outdated PinmemberVaSig21:58 7 Jul '09  
GeneralRe: [My vote of 2] Outdated PinmemberStephen Inglish7:36 17 Jul '09  
GeneralRe: [My vote of 2] Outdated [modified] PinmemberSigurd Johansen8:44 17 Jul '09  
GeneralRe: [My vote of 2] Outdated PinmemberMd. Marufuzzaman21:20 20 Jul '09  
Generalyour article has helped me so much! Pinmember7916711512:15 7 Jul '09  
GeneralNot Posting back PinmemberAgweet3:13 5 Aug '08  
GeneralRe: Not Posting back PinmemberSigurd Johansen8:15 10 Aug '08  
GeneralFix to the code Pinmemberobed216:48 13 Aug '07  
Questionhow to sending sms using asp.net vb script or c# Pinmembermalake20:58 27 Nov '06  
GeneralSolution to increasing refresh rate Pinmemberisikiss11:57 7 Aug '06  
GeneralRe: Solution to increasing refresh rate PinmemberSigurd Johansen10:26 7 Nov '06  
GeneralGood Pinmembervenkata krishnan K S23:23 12 Feb '06  
Generalaccess denied Pinmemberpaulains8:55 31 Dec '05  
GeneralAnother easy alternative Pinmemberplanoie5:53 8 Dec '05  
GeneralRe: Another easy alternative PinmemberUmer Khan21:46 16 May '07  
GeneralXmlHttpRequest PinmemberAquilax16:20 5 Dec '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120517.1 | Last Updated 20 Jul 2009
Article Copyright 2005 by Sigurd Johansen
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid