【问题标题】:Storing a Complex Object in Session State在会话状态中存储复杂对象
【发布时间】:2014-02-28 00:14:45
【问题描述】:

我的 App_Code 文件夹(.NET 项目)下有一个名为“StateBag.cs​​”的单独类

使用系统; 使用 System.Text;

[Serializable()]
public class MyStateBag
{
    public MyStateBag(){}
    private string _MemberID = string.Empty;
    public string MemberID
    {
        get { return _MemberID; }
        set { _MemberID = value; }
    }
}

现在我希望能够从我的 Web 项目的任何页面更新 MemberID 值。

例子:-

Default.aspx.cs:-

  public partial class _Default : System.Web.UI.Page
  {
    public StateBag MyStateBag
    {
        get { return (StateBag)Session["MyStateBag"]; }
        set { Session["MyStateBag"] = value; }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        this.MyStateBag = (StateBag)Session["MyStateBag"];
    }

    .... }

在我的 Default.aspx.cs 页面上,我尝试将 memberID 的值设置为 MyStateBag:-

 if (HttpContext.Current.Session["MyStateBag"] == null)
        {
            HttpContext.Current.Session["MyStateBag"] = new StateBag();
        }
        ((StateBag)HttpContext.Current.Session["MyStateBag"]).MemberID = memID;

但是我遇到了冲突:- “System.Web.UI.StateBag”不包含“MemberID”的定义,并且找不到接受“System.Web.UI.StateBag”类型的第一个参数的扩展方法“MemberID”(您是否缺少using 指令还是程序集引用?)

我是否必须在我的 Default.aspx.cs 页面上引用一些我错过的内容??

我怎样才能访问它...假设我在不同的页面上,“About.aspx.cs”。

我希望能够说:-memberinfo = StateBag["MemberID"]

谁能帮我理解这个?

【问题讨论】:

  • Nit: View State is not Session State 并且 [几乎总是] 绑定到 specific 页面(或者更确切地说,它存在于生成的HTML 和同时在服务器端重构)。要在不同页面之间保持状态,您可能对 Session State(既然使用了 Session,为什么要标记 ViewState?)、Cookie 或将状态编码到 Page URI 中感兴趣。
  • 太棒了!请问有示例代码吗?
  • 我试过了,但我的值没有被保存:- //HttpContext.Current.Session["memID"] = memID; //字符串成员信息 = string.Empty; //memberinfo = HttpContext.Current.Session["memID"].ToString();
  • 这是一个 ASP.NET 网站“项目”(文件->新网站,或添加->新网站),还是 ASP.NET Web 应用程序项目(文件->新项目或添加-> 新项目)?就代码文件而言,它会有所不同。
  • @JohnSaunders 它是一个网络应用程序。

标签: c# asp.net .net viewstate


【解决方案1】:

您保存到 Session 对象的任何内容都将在该用户浏览您的网站时保留在该用户身上。所以在 Default.aspx.cs 你可能有类似的东西:

//Make sure there is an object saved for this user
if(Session["StateBag"] == null)
   Session["StateBag"] = new StateBag();

//Set what you need
((StateBag)Session["StateBag"]).MemberID = 1234;

然后在另一个页面上你可以用同样的方式访问它

//Make sure there is an object saved for this user
if(Session["StateBag"] == null)
   Session["StateBag"] = new StateBag();

//Get what you need
int memberID = ((StateBag)Session["StateBag"]).MemberID

【讨论】:

  • 我敢打赌,您定义的 StateBag 类与 System.Web.UI.StateBag 类混淆了。尝试将您的 StateBag 重命名为其他独特的名称。
  • 所以我将 StateBag.cs​​ 更改为 MyStateBag.cs​​,其余代码现在引用 MyStateBag.cs​​。还是一样的冲突。如果您愿意再看一下,我更新了我的代码。
  • 您必须将其转换为 MyStateBag,而不是 StateBag,因为 StateBag 没有 MemberID 属性。
  • ((StateBag)HttpContext.Current.Session["MyStateBag"]).MemberID = memID;需要是 ((MyStateBag)HttpContext.Current.Session["MyStateBag"]).MemberID = memID;
  • 谢谢 Lee,现在我收到一个新错误:_Default.MyStateBag' is a 'property' but is used like a 'type'
猜你喜欢
  • 2012-12-09
  • 1970-01-01
  • 2017-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
相关资源
最近更新 更多