I have a separate class under my App_Code folder (.NET project) called "StateBag.cs"
using System; using System.Text;
[Serializable()]
public class MyStateBag
{
public MyStateBag(){}
private string _MemberID = string.Empty;
public string MemberID
{
get { return _MemberID; }
set { _MemberID = value; }
}
}
Now I want to be able to update the MemberID value from any page of my web project.
example:-
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"];
}
.... }
On my Default.aspx.cs Page I am trying to set the value of memberID into MyStateBag:-
if (HttpContext.Current.Session["MyStateBag"] == null)
{
HttpContext.Current.Session["MyStateBag"] = new StateBag();
}
((StateBag)HttpContext.Current.Session["MyStateBag"]).MemberID = memID;
However I get a conflict:- 'System.Web.UI.StateBag' does not contain a definition for 'MemberID' and no extension method 'MemberID' accepting a first argument of type 'System.Web.UI.StateBag' could be found (are you missing a using directive or an assembly reference?)
Do I have to reference something on my Default.aspx.cs page that I am missing out on??
and how can i access it back...say I am on a different page,"About.aspx.cs".
I want to be able to say :-memberinfo = StateBag["MemberID"]
Can anyone help me understand this?
Anything you save to the Session object will stay with that user as they travel around your site. So on Default.aspx.cs you might have something like:
//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;
Then on another page you can access it the same way
//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