一个关于.net 的问题

来源:百度知道 编辑:UC知道 时间:2024/05/28 23:13:45
protected void Page_Load(object sender, EventArgs e)
{
if (this.Session["userID"]== null)
{
Response.Redirect("Login.aspx");
}
else
{
if (!this.IsPostBack)
{
InitData();
}
}
}

解释一下这个吧!~~~~如果这是注册页面的*.cs会不会出问题!~~

我想应该是会加载不了注册页面的吧!~~~~

不能,应该改成这样
protected void Page_Load(object sender, EventArgs e)
{

if (!this.IsPostBack)
{ if (this.Session["userID"]== null)
{
Response.Redirect("Login.aspx");
}

InitData();
}

}

不能

不能加载 要想加载的话 if (this.Session["userID"]!= null)

1. 没有必要每次PostBack都进行检查,加载页面的时候判断一次就可以了。
2. Response.Redirect后方如果还有代码的时候,需要使用第二个参数并设置为false,否则在某些情况下会抛出异常。

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (this.Session["userID"]== null)
{
Response.Redirect("Login.aspx",false);
}
else
{
InitData();
}
}
}