我想重定向到一个新页面,但我想显示一条消息并有一个小的等待期,以便用户可以阅读它:
我知道我可以这样做:
<script runat="server">
protected override void OnLoad(EventArgs e)
{
Response.Redirect("new.aspx");
base.OnLoad(e);
}
</script>
但是我怎样才能显示消息并等待呢?
谢谢。
您可以使用元刷新在 html 中执行此操作,而不是使用服务器端代码:
<html>
<head>
<title> Redirect </title>
<meta http-equiv="refresh" content="60;URL='http://foo/new.aspx'"/>
</head>
<body>
<p>You will be redirected in 60 seconds</p>
</body>
</html>
您可以将 meta
标记的 content
属性中的 60 更改为您希望用户等待的秒数。
您可以使用客户端技术,例如元标记
<HTML>
<HEAD>
<!-- Send users to the new location. -->
<TITLE>redirect</TITLE>
<META HTTP-EQUIV="refresh"
CONTENT="10;URL=http://<URL>">
</HEAD>
<BODY>
[Your message here]
</BODY>
</HTML>
您是否尝试过使用元刷新标记?
文档可在此处找到:http://en.wikipedia.org/wiki/Meta_refresh
基本上,您将元刷新标记放在HTML的<head/>
部分中,并指定等待时间以及URL。
例如
<meta http-equiv="refresh" content="15;URL='http://www.something.com/page2.html'">
在上面的示例中,页面将等待 15 秒,然后重定向到 http://www.something.com/page2.html
。
因此,您可以创建一个包含您的消息的页面,并在其标题中放置元刷新。 在设定的时间段后,它将"刷新"到新的.aspx。
例如
<html>
<head>
<title>Redirecting</title>
<meta http-equiv="refresh" content="15;URL='new.aspx'">
</head>
<body>
<p>Thanks for visiting our site, you're about to be redirect to our next page. In the meantime, here's an interesting fact....</p>
</body>
</html>
您可以在查询字符串中传递消息和等待时间
Response.Redirect("new.aspx?Message=Your_Message&Time=3000")
在 new 的Page_Load中.aspx您可以捕获参数
string msg = Request["Message"]
string time = Request["Time"]
您需要用户等待 x 秒才能看到消息吗?如果是,您将需要通过JavaScript来完成。
首先,创建一个 javascript 函数来显示消息
function ShowMessage(msg) {
alert(msg);
}
然后,在 new.aspx 的代码中,获取参数并调用 JavaScript 函数
protected void Page_Load(object sender, EventArgs e)
{
string msg = Request["Message"].ToString();
string tempo = Request["Time"].ToString();
string script = String.Format(@"setTimeout(""ShowMessage('{0}')"", {1})", msg, tempo);
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "key", script, true);
}
它将在 3 秒后显示消息。