Response.write後馬上運行Response.redirect,Response.write沒反應
我們時常想達到這樣的效果:
Response.Write("<script>alert("您離線時間過長,請重新登陸!");</script>");
Response.Redirect("Login.aspx");
我們希望先提示信息,然後再跳轉頁麵。但這樣做的結果是:直接跳轉,沒有提示信息。
這是為什麼呢?
Response.Redirect方法之後,這個頁麵的內容根本就不會被發送到客戶端,而是直接在服務器端跳轉了,所以不管頁麵有什麼內容,客戶端都無法看到自然也無法運行。
怎麼修改呢?
方法一:
public class BasePage:System.Web.UI.Page
{
public BasePage()
{
this.Load += new EventHandler(BasePage_Load);
}
void BasePage_Load(object sender, EventArgs e)
{
if (Session["usernum"]==null)
{
Response.Write("<script languge='javascript'>alert('離線時間過長');
window.location.href='Login.aspx'</script>");
Response.End();
}
}
}
方法二(帶有參數):
Response.Write("<script languge='javascript'>alert('成功修改'); window.location.href='index.aspx?par =" + param + "'</script>);
Response.End();
為什麼說Response。End()不可少呢?請看我的另一篇博客。《利用Page事件進行統一身份驗證》。
https://blog.sina.com.cn/s/blog_67aaf4440100ms1c.html
最後更新:2017-04-02 22:16:08