Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以会话状态存储复杂对象_C#_Asp.net_.net_Viewstate - Fatal编程技术网

C# 以会话状态存储复杂对象

C# 以会话状态存储复杂对象,c#,asp.net,.net,viewstate,C#,Asp.net,.net,Viewstate,我在App_代码文件夹(.NET project)下有一个单独的类,名为“StateBag.cs” 使用制度; 使用系统文本 [Serializable()] public class MyStateBag { public MyStateBag(){} private string _MemberID = string.Empty; public string MemberID { get { return _MemberID; }

我在App_代码文件夹(.NET project)下有一个单独的类,名为“StateBag.cs”

使用制度; 使用系统文本

[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”]


有人能帮我理解这一点吗?

当用户在您的网站上四处走动时,您保存到会话对象的任何内容都将保留在该用户身边。因此,在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

Nit:视图状态不是会话状态,并且[几乎总是]绑定到特定页面(或者更确切地说,它存在于生成的HTML中,并且在重新构建服务器端时)。要在不同的页面上保持状态,您可能会对会话状态感兴趣(因为使用了会话,为什么要将此标记为ViewState?),Cookies,或者将状态编码到页面URI中。非常好!那么示例代码是什么呢?我尝试了这个方法,但是没有保存我的值:-//HttpContext.Current.Session[“memID”]=memID//string memberinfo=string.Empty//memberinfo=HttpContext.Current.Session[“memID”].ToString();这是ASP.NET网站“项目”(文件->新建网站,或添加->新建网站)还是ASP.NET Web应用程序项目(文件->新建项目或添加->新建项目)?就代码文件而言,它起着重要作用。@johnsa理解它是一个webapplication。我打赌您定义的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;谢谢李,现在我得到一个新的错误,它说:_Default.MyStateBag'是一个'property',但像'type'一样使用