简体   繁体   中英

Submit, Show Results, delay 3 seconds and redirect

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.

You can use Thread.Sleep() method. Just add this before the Response.Redirect ie

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.

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!

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/'">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM