Click here to Skip to main content
13,296,407 members (49,294 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 12 Nov 2013

ASP.NET timed page reload

, 13 Nov 2013
Rate this:
Please Sign up or sign in to vote.
ASP.Net timed page reload on button click/form submission.


After an ASP.NET form button action, display a thank you message, wait a certain amount of time and then reset (reload/redirect) the page.


The impetus for this was that I wanted to take a simpler approach to the user experience after a form completion than what I was used to.  I've most often seen examples where, after the submission, the user is redirected to another "Thank You" dummy page, which then after a time out redirects back to the original page.  It seemed to me that creating this extra dummy page was really just a waste of time, when all I really wanted was a thank you message to be displayed and to reset the form.   

The bottom line is, after completing a form on an ASP.NET page by pressing a button (I'm not using submit behavior), I wanted to update a label (on the same page) with a thank you message and have the page reload itself, thus resetting the form completely.  I could have easily just gotten some JavaScript code off the internet and called it a day.  However I wanted to see if I could possibly do it with what I had in hand within Visual Studio 2012 and AJAX Toolbox, rather than resorting to the easy way out of just copying someone else's code off the internet.  The following is the resulting method I came up with.

Using the code 

Step 1

First put a AJAX Timer control at the bottom of the page set to Disabled by default.  The Interval is set to 5000ms which is 5 seconds. The viewstate must be set to True, so that when the tick event happens, the control remains Enabled on the postback. (Don't forget the script manager control somewhere higher on the page!)

<asp:Timer ID="Timer1" runat="server" 

            Interval="5000" Enabled="False" EnableViewState="True"></asp:Timer>

Step 2

Add a label and a button to the page. In the code behind Click event for the button, set the label control with the message.  Next set the timer control to Enabled.  The Timer Tick event in the code behind doesn't need anything, but you could potentially use it for other actions if necessary.

<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Submit" />
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    'Display a submission message
    Label1.Text = _
    "Thank you for your request! " & _
    "<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" & _
    "This page will auto-reload in approx. 5 seconds."

    'Set display attributes for the submission message
    Label1.Font.Bold = True
    Label1.Font.Size = 12
    Label1.ForeColor = Color.Blue
    Label1.Font.Names = {"MS Trebuchet", "Tahoma", "Arial"}

    'Turn on the timer so the page will auto reload
    Timer1.Enabled = True

End Sub

Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        'do nothing, the necessary work happens on the postback page load
End Sub

Step 3

In the code behind file in the Page Load event, I check for postback and then check if the Timer is Enabled.  If the Timer is Enabled, we know we need to reload the page, because the Timer is never enabled unless the Submit button was pressed.  We disable the Timer and then do a Redirect, to the page itself.

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

   If IsPostBack = True Then
       If Timer1.Enabled = True Then
           Timer1.Enabled = False
       End If
   End If
End Sub

Points of Interest

May not be the most elegant method given your doing a postback and then a redirect.  However from the user perspective, it just appears the page reloaded, which is the desired experience.


Let's just call it version 1.0.  :)


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


About the Author

James Gabel
Systems / Hardware Administrator Financial Sector
United States United States
IT Professional with over 15+ years of supporting Microsoft based systems and over 6+ years of supporting linux based systems. Systems Admininstrator specializing in LDAP systems (ie. AD, eDir, UnboundID, etc.) for a large financial services sector company. Also an operating system enthusiast and a "drive by coder".(aka 'pretty good novice') I also do scripting/automation and coding on an as needed basis. As of late having fun learning, ms-sql and wcf.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 13 Nov 2013
Article Copyright 2013 by James Gabel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid