Submit, Show Results, delay 3 seconds and redirect

。_饼干妹妹 提交于 2020-01-24 06:06:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!