472,111 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,111 software developers and data experts.

Viewstate in Session

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
Nov 19 '05 #1
3 1991
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" <m@m.com> wrote in message
news:O9**************@TK2MSFTNGP11.phx.gbl...
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

Nov 19 '05 #2
that looks ok to me

--
Regards,
Alvin Bruney
"MattC" <m@m.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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" <m@m.com> wrote in message
news:O9**************@TK2MSFTNGP11.phx.gbl...
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


Nov 19 '05 #3
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 [MVP]" <vapor at steaming post office> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
that looks ok to me

--
Regards,
Alvin Bruney
"MattC" <m@m.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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" <m@m.com> wrote in message
news:O9**************@TK2MSFTNGP11.phx.gbl...
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



Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by neo | last post: by
1 post views Thread by Ralph Soons | last post: by
6 posts views Thread by clsmith66 | last post: by
6 posts views Thread by Max | last post: by
6 posts views Thread by mosscliffe | last post: by
2 posts views Thread by dotComPaJi | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.