Viewstate in Session

M

MattC

With respect to Peter Bromberg's article (
http://www.eggheadcafe.com/articles/20040613.asp ) I wish to store my
viewstate info in the Session object rather than the cache (as it is per
user info). I have tried the following but am unable to serialize the
viewstate??

protected override void SavePageStateToPersistenceMedium(object viewState)
{
string str = "VIEWSTATE_" + Request.UserHostAddress + "_" +
DateTime.Now.Ticks.ToString();
Session[str] = viewState;
RegisterHiddenField("__VIEWSTATE_KEY", str);
RegisterHiddenField("__VIEWSTATE", "");
}
protected override object LoadPageStateFromPersistenceMedium()
{
string str = Request.Form["__VIEWSTATE_KEY"];
if (!str.StartsWith("VIEWSTATE_"))
{
throw new Exception("Invalid viewstate key:" + str);
}
return Session[str];
}

TIA

MattC
 
M

MattC

I hunted around a bit and got this to work, however I am unsure of its
implications.

protected override void SavePageStateToPersistenceMedium(object viewState)
{
LosFormatter oLosFormatter = new LosFormatter();
StringWriter oStringWriter = new StringWriter();
oLosFormatter.Serialize(oStringWriter, viewState);
string str = "VIEWSTATE_" + Request.UserHostAddress + "_" +
DateTime.Now.Ticks.ToString();
Session[str] = oStringWriter.ToString();
RegisterHiddenField("__VIEWSTATE_KEY", str);
RegisterHiddenField("__VIEWSTATE", "");
}
protected override object LoadPageStateFromPersistenceMedium()
{
LosFormatter oLosFormatter = new LosFormatter();
string str = Request.Form["__VIEWSTATE_KEY"];
if (!str.StartsWith("VIEWSTATE_"))
{
throw new Exception("Invalid viewstate key:" + str);
}
return oLosFormatter.Deserialize(Session[str].ToString());
}
 
A

Alvin Bruney [MVP]

that looks ok to me

--
Regards,
Alvin Bruney
MattC said:
I hunted around a bit and got this to work, however I am unsure of its
implications.

protected override void SavePageStateToPersistenceMedium(object viewState)
{
LosFormatter oLosFormatter = new LosFormatter();
StringWriter oStringWriter = new StringWriter();
oLosFormatter.Serialize(oStringWriter, viewState);
string str = "VIEWSTATE_" + Request.UserHostAddress + "_" +
DateTime.Now.Ticks.ToString();
Session[str] = oStringWriter.ToString();
RegisterHiddenField("__VIEWSTATE_KEY", str);
RegisterHiddenField("__VIEWSTATE", "");
}
protected override object LoadPageStateFromPersistenceMedium()
{
LosFormatter oLosFormatter = new LosFormatter();
string str = Request.Form["__VIEWSTATE_KEY"];
if (!str.StartsWith("VIEWSTATE_"))
{
throw new Exception("Invalid viewstate key:" + str);
}
return oLosFormatter.Deserialize(Session[str].ToString());
}
MattC said:
With respect to Peter Bromberg's article (
http://www.eggheadcafe.com/articles/20040613.asp ) I wish to store my
viewstate info in the Session object rather than the cache (as it is per
user info). I have tried the following but am unable to serialize the
viewstate??

protected override void SavePageStateToPersistenceMedium(object
viewState)
{
string str = "VIEWSTATE_" + Request.UserHostAddress + "_" +
DateTime.Now.Ticks.ToString();
Session[str] = viewState;
RegisterHiddenField("__VIEWSTATE_KEY", str);
RegisterHiddenField("__VIEWSTATE", "");
}
protected override object LoadPageStateFromPersistenceMedium()
{
string str = Request.Form["__VIEWSTATE_KEY"];
if (!str.StartsWith("VIEWSTATE_"))
{
throw new Exception("Invalid viewstate key:" + str);
}
return Session[str];
}

TIA

MattC
 
M

MattC

Is there anyway to place this information in the Cache instead (so I can set
an expiry) but where it is used per person. Using this method I realised
that the session will grow very large after a while and the session timeout
might not be quick enough.
At the moment it is appending the current Tick of the system clock, so each
request would use its own entry in the session.
string str = "VIEWSTATE_" + Request.UserHostAddress + "_" +
DateTime.Now.Ticks.ToString();

If I alter to:
string str = "VIEWSTATE_" + Request.UserHostAddress;
I can avoid this problem. However, what are the issue involved in resizing
the space required by an object in the Session.

TIA

MattC

Alvin Bruney said:
that looks ok to me

--
Regards,
Alvin Bruney
MattC said:
I hunted around a bit and got this to work, however I am unsure of its
implications.

protected override void SavePageStateToPersistenceMedium(object
viewState)
{
LosFormatter oLosFormatter = new LosFormatter();
StringWriter oStringWriter = new StringWriter();
oLosFormatter.Serialize(oStringWriter, viewState);
string str = "VIEWSTATE_" + Request.UserHostAddress + "_" +
DateTime.Now.Ticks.ToString();
Session[str] = oStringWriter.ToString();
RegisterHiddenField("__VIEWSTATE_KEY", str);
RegisterHiddenField("__VIEWSTATE", "");
}
protected override object LoadPageStateFromPersistenceMedium()
{
LosFormatter oLosFormatter = new LosFormatter();
string str = Request.Form["__VIEWSTATE_KEY"];
if (!str.StartsWith("VIEWSTATE_"))
{
throw new Exception("Invalid viewstate key:" + str);
}
return oLosFormatter.Deserialize(Session[str].ToString());
}
MattC said:
With respect to Peter Bromberg's article (
http://www.eggheadcafe.com/articles/20040613.asp ) I wish to store my
viewstate info in the Session object rather than the cache (as it is per
user info). I have tried the following but am unable to serialize the
viewstate??

protected override void SavePageStateToPersistenceMedium(object
viewState)
{
string str = "VIEWSTATE_" + Request.UserHostAddress + "_" +
DateTime.Now.Ticks.ToString();
Session[str] = viewState;
RegisterHiddenField("__VIEWSTATE_KEY", str);
RegisterHiddenField("__VIEWSTATE", "");
}
protected override object LoadPageStateFromPersistenceMedium()
{
string str = Request.Form["__VIEWSTATE_KEY"];
if (!str.StartsWith("VIEWSTATE_"))
{
throw new Exception("Invalid viewstate key:" + str);
}
return Session[str];
}

TIA

MattC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,045
Members
48,769
Latest member
Clifft

Latest Threads

Top