我想对每个页面都判断一下是否登陆,网上找的方法是加一个BasePage类,然后再加到每个页面上,但是总是404错误,求解决办法??
BasePage类:
- C# code
public class BasePage : System.Web.UI.Page { public BasePage() { this.Load += new EventHandler(BasePage_Load); } void BasePage_Load(object sender, EventArgs e) { if (Session["username"] == null||Session["username"].ToString().Equals(" ")) { Response.Write("<script>alert('请先登录!');window.location='../Default.aspx';</script>"); } } }
在调用页面的CS中写了:
- C# code
public partial class _Default : BasePage { protected void Page_Load(object sender, EventArgs e) { .......... } }
怎么办呢???
------解决方案--------------------------------------------------------
System.Web.HttpContext.Current.Response.Write("<script>alert('请先登录!');window.location='" + VirtualPathUtility.ToAbsolute("~/") + "Default.aspx';</script>");
或者
System.Web.HttpContext.Current.Response.Write("<script>alert('请先登录!');window.location='" + Page.ResolveUrl("~/") + "Default.aspx';</script>");