问题
I've got a form where users can input data and the behavior that I'm looking for is after the submit, the code behind writes to a database and returns a response of success to the page. I currently have this code working. The next item that I'd like to have happen is redirect after the successful message but I'd like to delay for 2 or 3 seconds allowing the user to see the success message before redirecting.
Behavior: Submit -> Show Success Message -> Delay 3 seconds -> Redirect
I'm currently developing this in VB; however I'm fine with examples in either VB or C#. This is a traditional web form.
回答1:
I like this approach, although some of the already proposed are very good:
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "success", "alert('Submitted Successfully.'); setInterval(function(){location.href='http://www.google.com';},3000);", true);
}
Good luck!
回答2:
You can use Thread.Sleep() method. Just add this before the Response.Redirect i.e.
System.Threading.Thread.Sleep(3000);
Response.Redirect("MyURL");
Alternatively, You could add delay in Response header like this
Response.AddHeader "REFRESH","3;URL=MyURL"
The number denotes above is delay value in seconds.
回答3:
You can use html meta refresh tag
Just place the following cod in head section on your page containing success message.
<meta http-equiv="refresh" content="3;URL='http://nextpageurlaftersuccess.com/'">
来源:https://stackoverflow.com/questions/12219246/submit-show-results-delay-3-seconds-and-redirect