asp.net中session怎么不能保存状态

来源:百度知道 编辑:UC知道 时间:2024/06/24 10:52:39
public partial class UserDefault : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Response.Write("<script>alert("+Session["userInfo"]+");</script>");

User usr = Session["userInfo"] as User;
if (usr != null)
{
this.pnLogin.Visible = false;
this.pnManager.Visible = true;
}
else
{
this.pnLogin.Visible = true;
this.pnManager.Visible = false;
this.onLoginmess.Text = "游客!您好!";
}
}
protected void imgLogin_Click1(object sender, ImageClickEventArgs e)
{
string loginid = this.txtUsername.Value;
string loginpwd = this.txtPassword.Value;
User userout = new User();
bool

protected void imgLogin_Click1(object sender, ImageClickEventArgs e)
{
string loginid = this.txtUsername.Value;
string loginpwd = this.txtPassword.Value;
User userout = new User();
bool flag = UserManager.Login(loginid, loginpwd, out userout);
if (flag)
{
this.pnLogin.Visible = false;
Session["userInfo"] = userout;
Response.Write(((User)Session["userInfo"]).Name);
Session.Timeout = 30;
this.pnManager.Visible = true;
this.lblMessage.Text = userout.Name.ToString() + "欢迎您!";
}

这里有问题,你仔细看啊,你实例化了一个空的userout 自然session是空的啊!
解决方法是:
string loginid = this.txtUsername.Value;
string loginpwd = this.txtPassword.Value;
User userout = new User();
userout.username=loginid;
userout.password=loginpwd;
//userout 这个就要看你是怎么定义的了,username和password是我模拟出来的。

恩,没注意看,你是用out把他弄出来的,那么你在
User user = UserService.Register(loginId);