页面操作完成时,显示提示信息并倒计N秒后跳转(草)
1. 当前页,在后台代码往前台输出(注册)脚本的方式
A:一般操作成功后提示(这里直接用Response.Write输出了,也可以使用Page的ClientScript对象的RegisterStartupScript方法)
protected void Button1_Click(object sender, EventArgs e)
{
string yourUrl = "http://www.baidu.com";
string yourMsg = "操作已成功";
StringBuilder js = new StringBuilder("<script language=\"javascript\">")
.Append("document.write(\'" + yourMsg + "\');")
.Append("var timeSpan = 10; setInterval(\"Redirect()\",1000);")
.Append("function Redirect(){ if(timeSpan == 0){")
.Append("window.location.href=\""+yourUrl+"\"; }else{")
.Append("document.body.innerHTML = \"倒数\"+(timeSpan--)+\"秒\";}}")
.Append("</script>");
Response.Write(js.ToString());
}
{
string yourUrl = "http://www.baidu.com";
string yourMsg = "操作已成功";
StringBuilder js = new StringBuilder("<script language=\"javascript\">")
.Append("document.write(\'" + yourMsg + "\');")
.Append("var timeSpan = 10; setInterval(\"Redirect()\",1000);")
.Append("function Redirect(){ if(timeSpan == 0){")
.Append("window.location.href=\""+yourUrl+"\"; }else{")
.Append("document.body.innerHTML = \"倒数\"+(timeSpan--)+\"秒\";}}")
.Append("</script>");
Response.Write(js.ToString());
}
B:异常处理 发生时提示
try
{
int.Parse("hello");
}
catch (Exception ex)
{
StringBuilder js = new StringBuilder("<script language=\"javascript\">")
.Append("document.write(\'" + ex.Message + "\');")
.Append("var timeSpan = 10; setInterval(\"Redirect()\",1000);")
.Append("function Redirect(){ if(timeSpan == 0){")
.Append("window.location.href=\"http://www.baidu.com\"; }else{")
.Append("document.body.innerHTML = \"倒数\"+(timeSpan--)+\"秒\";}}")
.Append("</script>");
Response.Write(js.ToString());
}
{
int.Parse("hello");
}
catch (Exception ex)
{
StringBuilder js = new StringBuilder("<script language=\"javascript\">")
.Append("document.write(\'" + ex.Message + "\');")
.Append("var timeSpan = 10; setInterval(\"Redirect()\",1000);")
.Append("function Redirect(){ if(timeSpan == 0){")
.Append("window.location.href=\"http://www.baidu.com\"; }else{")
.Append("document.body.innerHTML = \"倒数\"+(timeSpan--)+\"秒\";}}")
.Append("</script>");
Response.Write(js.ToString());
}
2. 跳转到另外一页(带需要的参数及值),然后在新页中处理
A:使用JS,可以参考一中的脚本,用js读取参数就可以了
B:如果无参数传递,可以考虑在<head></head>里插入
<meta http-equiv="refresh" content="等待秒数;url=跳转地址">