- 有一个 Site.Master (Masterpage)
- 得到Default.aspx(页面)
- 在Default.aspx中,放置了一个UserControl LoginUserControl.ascx
- Got a Site.Master (Masterpage)
- Got Default.aspx (Page)
- And in the Default.aspx, there is a UserControl LoginUserControl.ascx placed
现在我的问题:
在 LoginUserControl 中,我检查登录是否正确.如果是,那么我将 Default.aspx 上的属性 IsLoggedIn 设置为 true:
In the LoginUserControl I check if Login is right. If yes, then I set the Property IsLoggedIn on the Default.aspx to true:
//Inside LoginUserControl.ascx if (/*Login is Ok*/) { ((Default)Page).IsLoggedIn = true; }所以,现在我需要在我的 Masterpage Site.Master 中提供这些信息我必须知道用户是否登录..我这样做:
So, now I need this Information in my Masterpage Site.Master I must know if User is logged in or not.. I do this:
//Inside Site.Master protected void Page_Load(object sender, EventArgs e) { if (((Default)Page).IsLoggedIn) { //Do Something } }但它总是是假的!为什么?我以为我设置了 IsLoggedIn = true ?!那为什么是假的呢?这是生命周期问题吗,我必须做什么,它可以工作:(
But its ALWAYS false! Why? I thought I set the IsLoggedIn = true ?! Why is it then false? Is it a Lifecycle problem and what I must do, that it works :(
推荐答案您应该将 IsLogged 属性添加到页面的视图状态.
You should add the property IsLogged on to the viewstate of the page.
public bool IsLoggedOn { get { return ViewState["IsLoggedOn"]==null?false:Convert.ToBoolean(ViewState["IsLoggedOn"]); } set { ViewState["IsLoggedOn"] = value; } }或者如果该属性在多个页面上使用,您应该将其添加到会话中(用会话替换 ViewState)
Or if the property is used over multiple pages you should add it to the Session (replace ViewState with Session)
更多推荐
ASP.NET UserControl 变量寿命问题
发布评论