Click here to Skip to main content
15,884,099 members
Articles / Web Development / ASP.NET
Tip/Trick

ASP.NET timed page reload

Rate me:
Please Sign up or sign in to vote.
4.25/5 (5 votes)
13 Nov 2013CPOL2 min read 20.1K   7  
ASP.Net timed page reload on button click/form submission.

Introduction  

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.

Background 

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.NET
<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.NET
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Submit" />
VB.NET
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.

VB.NET
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
           Response.Redirect(Request.RawUrl)
       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.

History  

Let's just call it version 1.0.  :)

License

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


Written By
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 asp.net, ms-sql and wcf.

Comments and Discussions

 
-- There are no messages in this forum --