473,408 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Using Request.Cookies or ViewState

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.
Nov 19 '05 #1
4 1665
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.

-Brock
DevelopMentor
http://staff.develop.com/ballen
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.


Nov 19 '05 #2
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.
Nov 19 '05 #3
> 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.

-Brock
DevelopMentor
http://staff.develop.com/ballen


Nov 19 '05 #4
>> 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?
Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bhupesh Saini | last post by:
I am trying to call a ASPX page using HttpWebRequest class and pass cookie information to it. My ASPX pages gets called just fine, however none of the request cookies are available to the ASPX page....
7
by: isaac2004 | last post by:
hi i have a basic asp page that acts as an online bookstore. on my cart page i am having trouble generating 3 numbers; a subtotal, a shipping total, and a final price. here is my code i would...
2
by: barrybevel | last post by:
Hi, I have a very small simple program below which does the following: 1) post a username & password to a website - THIS WORKS 2) follow a link - THIS WORKS 3) update values of 2 fields and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.