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: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
Label1.Text = _
"Thank you for your request! " & _
"<br /><br /> " & _
"This page will auto-reload in approx. 5 seconds."
Label1.Font.Bold = True
Label1.Font.Size = 12
Label1.ForeColor = Color.Blue
Label1.Font.Names = {"MS Trebuchet", "Tahoma", "Arial"}
Timer1.Enabled = True
End Sub
Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
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
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. :)