C# 使用C显示消息后重定向页面#
我只是尝试先显示消息,然后将用户重定向到另一个页面。我遇到的问题是,它没有首先显示消息,但它将页面重定向到正确的方向。 这是我的密码C# 使用C显示消息后重定向页面#,c#,asp.net,C#,Asp.net,我只是尝试先显示消息,然后将用户重定向到另一个页面。我遇到的问题是,它没有首先显示消息,但它将页面重定向到正确的方向。 这是我的密码 if (some condition == true) { string message = string.Empty; message = "Success. Please check your email. Thanks."; ClientScript.RegisterStartupScript(GetType(), "alert"
if (some condition == true)
{
string message = string.Empty;
message = "Success. Please check your email. Thanks.";
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
Response.Redirect("Login.aspx");
}
这是因为代码从服务器端重定向,甚至在脚本到达客户端浏览器之前。您应该删除该重定向并修改javascript,以便在显示该消息后在客户端完成重定向
编辑:你一定要检查一下。这里的问题是你在做两件事:
alert('Message');
window.location.href = 'login.aspx';
因此,您需要删除响应。重定向行并使用:
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "'); window.location.href = 'login.aspx'", true);
实现结果的最佳方法是使用Javascript(客户端)异步执行
如果您希望在服务器端执行,下面是一个示例:
protected void btnRedirect_Click(object sender, EventArgs e)
{
string message = "You will now be redirected to YOUR Page.";
string url = "http://www.yourpage.com/";
string script = "window.onload = function(){ alert('";
script += message;
script += "');";
script += "window.location = '";
script += url;
script += "'; }";
ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
}
你可以在C#中使用计时器。只需给用户足够的时间阅读消息,然后将用户重定向到所需页面
此线程在使用计时器方面有一个很好的示例:这是否回答了您的问题?