ASP.NET UserControl 变量寿命问题

编程入门 行业动态 更新时间:2024-10-08 10:56:43
本文介绍了ASP.NET UserControl 变量寿命问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述
  • 有一个 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 变量寿命问题

本文发布于:2023-11-11 08:40:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   寿命   ASP   NET   UserControl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!