在我的应用程序中,我有一个按钮来保存一些信息。但是,我想在执行最后一行之前延迟代码,以便用户可以在重定向到新页面之前阅读显示的消息。
我知道这样做并不是一种最佳方式,但出于某些原因(例如时间)我还是想做到这一点。
有可能吗?如果是的话,我怎么能这样做?
提前致谢!
protected void SaveButton_Click(object sender, EventArgs e) {
// Lots of code not relevant for the problem here
Service service = new Service();
service.SaveMovie(movie);
successMessage.Visible = true;
happyMessage.Text = "The movie was successfully added, now add some genres!";
// Here I want a delay of 2 seconds before the next line is executed...
Response.Redirect(String.Format("~/Edit.aspx?id={0}", movie.MovieID), false);
}
答案 0 :(得分:1)
您需要在客户端执行此操作。另一种选择是:
在名为Javascript
的页面中定义redirect
函数,如下所示:
function redirect(url)
{
setTimeout(function(){window.location.href=url;} ,2000);
}
protected void SaveButton_Click(object sender, EventArgs e)
{
// Lots of code not relevant for the problem here
Service service = new Service();
service.SaveMovie(movie);
successMessage.Visible = true;
happyMessage.Text = "The movie was successfully added, now add some genres!";
// Here I want a delay of 2 seconds before the next line is executed...
ClientScript.RegisterStartupScript(this.GetType(),"somekey","redirect('"+String.Format("~/Edit.aspx?id={0}", movie.MovieID)+"');");
}
答案 1 :(得分:0)
如果您使用的是Javascript,这将很容易。使用javascript将提升性能
Button_Click
{
string js ="<script type='text/javascript'>setTimeout(function()window.location.href="+String.Format("~/Edit.aspx?id={0}", movie.MovieID)+";} ,2000);</script>"
ScriptManager.RegisterStartupScript(Me.Page,GetType(Page),“js”,js,False)
}