Using Request.Cookies or ViewState

B

BobRoyAce

I have a C# ASP.NET page that I inherited which has two panels on it. It
only ever shows one or the other. Initially, it shows panel one. Then, if
the user clicks on a certain button, it ultimately shows the other (after a
postback, of course). What I need to do it put code into place that will
allow code in the Page_Load event, in the case of a PAGE REFRESH (NOTE: Page
involved is stored in a FRAME which is part of a FRAMESET), to know which
panel to show. What is the best way to accomplish something like this?
Should I use Request.Cookies["Panel"] or ViewState["Panel"]?

I tried the latter, but after clicking on a button, resulting in showing
Panel2, and then refreshing the page, ViewState["Panel"] was null. I didn't
explicitly set it to null. What events result in ViewState items being set
to null?

A problem I was having with the Request.Cookies way of doing things was that
I was trying to check to see, in the Page_Load event, where IsPostBack was
false, if the Request.Cookies["Panel"] cookie was already set (i.e. not
null) and the code was failing telling me "Object reference not set to an
instance of an object." The line of code that produced this error was as
follows:

if (Request.Cookies["Panel"] != null)

Any insights would be appreciated.
 
B

Brock Allen

ViewState will only be "remembered" during a postback sequence. If they do
a GET (refresh), then as you noticed the ViewState is empty. A cookie is
an ok way to do this. The only thing is that it's passed back to every page
the user visits... So I'd suggest giving the cookie a timeout so it eventually
gets purged in case they don't come back to your page with the panel.
 
B

BobRoyAce

Thanks Brock...is there a way to like set the cookie to Null when the page
is "left". If the user clicks on a button that does a REDIRECT, I know I
could just set it to NULL there (i.e. in the click event). However, what if
they click on a hyperlink (in another frame), or type in a URL, to go to
another page?

By the way, how about using Session["Panel"]. Using that seemed to mostly
work except that all the labels on Panel2 that previously displayed data now
displayed none. What are the pros/cons of using that rather than
Request.Cookies["Panel"]? Any input would be appreciated.
 
B

Brock Allen

Thanks Brock...is there a way to like set the cookie to Null when the
page is "left". If the user clicks on a button that does a REDIRECT, I
know I could just set it to NULL there (i.e. in the click event).
However, what if they click on a hyperlink (in another frame), or type
in a URL, to go to another page?

Not unless on that other page you get rid of the cookie. There are other
ways to deal with this, such as adding code into global.asax in Application_EndRequest
say to remove the cookie if the request isn't on the page you want the information
to be remembered. But this is starting to feel like overkill. There's never
a silver bullet is there? :)
By the way, how about using Session["Panel"]. Using that seemed to
mostly work except that all the labels on Panel2 that previously
displayed data now displayed none. What are the pros/cons of using
that rather than Request.Cookies["Panel"]? Any input would be
appreciated.

You could use Session as an alternate way to store this information. This
will certainly timeout though such that when the user returns tomorrow their
last choice will have been forgotten. Also, unless you configure session
state to be stored out of process, then this wont work in a server farm environment.
 
B

BobRoyAce

By the way, how about using Session["Panel"]. Using that seemed to
mostly work except that all the labels on Panel2 that previously
displayed data now displayed none. What are the pros/cons of using
that rather than Request.Cookies["Panel"]? Any input would be
appreciated.

You could use Session as an alternate way to store this information. This
will certainly timeout though such that when the user returns tomorrow
their last choice will have been forgotten. Also, unless you configure
session state to be stored out of process, then this wont work in a server
farm environment.

I'm not concerned with the timeout issue. The fact that, unless configured
correctly, it won't work in a server farm environment isn't relevant today,
but may be in the future. Thanks for the insights.

By the way, any good recommendations for books and/or web sites related to
these "state" type topics?
 

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

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,117
Latest member
Matilda564
Top