473,803 Members | 3,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ViewState v Session - Confused

I am testing for how/when a page is posted back and I decided to use a
ViewState variable in PageLoad to set up a counter, but it appears,
the ViewState is cleared on each PageLoad. So then I used SESSION and
that worked.

Am I correct in assuming ViewState is cleared on each PageLoad or is my
code incorrect.

VIEWSTATE
If IsNothing(ViewS tate("PbCounter ")) Then

ViewState("PBCo unter") = 1
Else
ViewState("PBCo unter") = Session("PBCoun ter") + 1
End If

SESSION
If IsNothing(Sessi on("PbCounter") ) Then

Session("PBCoun ter") = 1
Else
Session("PBCoun ter") = Session("PBCoun ter") + 1
End If

Response.write( ViewState("PBCo unter").tostrin g) ALWAYS ONE

Response.write( Session("PBCoun ter").tostring ) INCREMENTS CORRECTLY

Thanks - Richard

May 10 '06 #1
6 1603
If this is really your code, then you are putting an empty Session
variable into the Viewstate.

VIEWSTATE
If IsNothing(ViewS tate("PbCounter ")) Then
ViewState("PBCo unter") = 1
Else
ViewState("PBCo unter") = Session("PBCoun ter") + 1 <<<<<<<
HERE
End If

May 10 '06 #2
Sorry Typo - with cut and paste, no it was correct in the actual code.
I did viewstate first and when it did not work I then changed it to
session.

May 10 '06 #3
ViewState is stored in a Page, and passed back and forth to the client via a
hidden form field. Session is stored in Server memory. Therefore, ViewState
is scoped to a Page, but only to the Page after PostBack, not when it is
initially loaded. Session State is scoped to a single user Session, and
transcends Pages.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"mosscliffe " <pa***********@ googlemail.com> wrote in message
news:11******** ************@j7 3g2000cwa.googl egroups.com...
I am testing for how/when a page is posted back and I decided to use a
ViewState variable in PageLoad to set up a counter, but it appears,
the ViewState is cleared on each PageLoad. So then I used SESSION and
that worked.

Am I correct in assuming ViewState is cleared on each PageLoad or is my
code incorrect.

VIEWSTATE
If IsNothing(ViewS tate("PbCounter ")) Then

ViewState("PBCo unter") = 1
Else
ViewState("PBCo unter") = Session("PBCoun ter") + 1
End If

SESSION
If IsNothing(Sessi on("PbCounter") ) Then

Session("PBCoun ter") = 1
Else
Session("PBCoun ter") = Session("PBCoun ter") + 1
End If

Response.write( ViewState("PBCo unter").tostrin g) ALWAYS ONE

Response.write( Session("PBCoun ter").tostring ) INCREMENTS CORRECTLY

Thanks - Richard

May 10 '06 #4
Thanks. That was helpful information.

--
Greg Collins [InfoPath MVP]
Visit Brain Trove ( http://www.BrainTrove.com )
Visit InfoPath Dev ( http://www.InfoPathDev.com )
May 10 '06 #5
Eh?

On first page request you should be able to add custom values.
On postback you should be able to obtain it again.
Am i missing something here, i believe i thave tested this a while ago.
Of course a viewstate is bounded to a page but that could be welcome.
+ when using a master page you could tag stuff to multiple 'pages' (imo)

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> schreef in bericht
news:ek******** ******@TK2MSFTN GP05.phx.gbl...
ViewState is stored in a Page, and passed back and forth to the client via
a hidden form field. Session is stored in Server memory. Therefore,
ViewState is scoped to a Page, but only to the Page after PostBack, not
when it is initially loaded. Session State is scoped to a single user
Session, and transcends Pages.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"mosscliffe " <pa***********@ googlemail.com> wrote in message
news:11******** ************@j7 3g2000cwa.googl egroups.com...
I am testing for how/when a page is posted back and I decided to use a
ViewState variable in PageLoad to set up a counter, but it appears,
the ViewState is cleared on each PageLoad. So then I used SESSION and
that worked.

Am I correct in assuming ViewState is cleared on each PageLoad or is my
code incorrect.

VIEWSTATE
If IsNothing(ViewS tate("PbCounter ")) Then

ViewState("PBCo unter") = 1
Else
ViewState("PBCo unter") = Session("PBCoun ter") + 1
End If

SESSION
If IsNothing(Sessi on("PbCounter") ) Then

Session("PBCoun ter") = 1
Else
Session("PBCoun ter") = Session("PBCoun ter") + 1
End If

Response.write( ViewState("PBCo unter").tostrin g) ALWAYS ONE

Response.write( Session("PBCoun ter").tostring ) INCREMENTS CORRECTLY

Thanks - Richard


May 10 '06 #6
Yes, you can add values to it initially, but it is not stored. It is stored
in a hidden form field, which, when the page Posts Back, re-creates the
ViewState object from the field contents. In other words, if you add
something to a Page's ViewState, and navigate back to the same Page by any
means other then a PostBack, the ViewState will be empty.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"Edwin Knoppert" <in**@pbsoft.sp eedlinq.nl> wrote in message
news:e3******** **@azure.qinip. net...
Eh?

On first page request you should be able to add custom values.
On postback you should be able to obtain it again.
Am i missing something here, i believe i thave tested this a while ago.
Of course a viewstate is bounded to a page but that could be welcome.
+ when using a master page you could tag stuff to multiple 'pages' (imo)

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> schreef in bericht
news:ek******** ******@TK2MSFTN GP05.phx.gbl...
ViewState is stored in a Page, and passed back and forth to the client
via a hidden form field. Session is stored in Server memory. Therefore,
ViewState is scoped to a Page, but only to the Page after PostBack, not
when it is initially loaded. Session State is scoped to a single user
Session, and transcends Pages.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"mosscliffe " <pa***********@ googlemail.com> wrote in message
news:11******** ************@j7 3g2000cwa.googl egroups.com...
I am testing for how/when a page is posted back and I decided to use a
ViewState variable in PageLoad to set up a counter, but it appears,
the ViewState is cleared on each PageLoad. So then I used SESSION and
that worked.

Am I correct in assuming ViewState is cleared on each PageLoad or is my
code incorrect.

VIEWSTATE
If IsNothing(ViewS tate("PbCounter ")) Then

ViewState("PBCo unter") = 1
Else
ViewState("PBCo unter") = Session("PBCoun ter") + 1
End If

SESSION
If IsNothing(Sessi on("PbCounter") ) Then

Session("PBCoun ter") = 1
Else
Session("PBCoun ter") = Session("PBCoun ter") + 1
End If

Response.write( ViewState("PBCo unter").tostrin g) ALWAYS ONE

Response.write( Session("PBCoun ter").tostring ) INCREMENTS CORRECTLY

Thanks - Richard



May 10 '06 #7

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

Similar topics

10
2288
by: neo | last post by:
hi, I am studying ASP.NET and have few questions - 1) The session ID and values of controls is stored in VIEWSTATE variable. So now when we put EnableViewState="false" in Page directive and disable the session state in Web.Config the VIEWSTATE variable is still maintained and stores some values. Can anyone tell what those values are for, i.e what other info is stored in VIEWSTATE other than the session ID and the control values ?
4
6732
by: Chuck Ritzke | last post by:
Hi, I've searched the newsgroup and other sources to understand how to handle runtime controls and see I'm not the only one who's confused, but I'm still not quite sure of the best way to handle from all the various explanations/answers. I'm attempting the typical scenario... I create a variable number of controls at runtime based on parameters from a database.
6
4349
by: clsmith66 | last post by:
Is it possible to store the same information about a control that would be saved in the ViewState in a Session state? I have a page with three treeview controls and if I enable the view state for the controls I get huge delays on postbacks, but if I disable the viewstate I lose all my information. Any sugesstions?
3
4113
by: Alex Nitulescu | last post by:
Sorry, I'm really confused - I'm only at the middle of my book and already I can count 4 methods to store values. Yes, I know that cookies can not store much, but what is the difference between storing data in the cache object, in a viewstate and in a Session. I can say Cache("MyData")=objDataSet as well as
3
4577
by: RCS | last post by:
I have an app that I have different "sections" that I want to switch back and forth from, all while having the server maintain viewstate for each page. In other words, when I am on Page1.aspx and set textboxes, radio buttons, etc - that viewstate is fine. Then I have a linkbutton that does a Server.Transfer over to Page2.aspx. When I Server.Transfer back to Page1.aspx, the viewstate info is lost. I ran across another example of this last...
9
1873
by: Mark Broadbent | last post by:
Been a while since I've touched asp.net but one thing that always seems to fustrate me is the loss of state on variable declarations. Is there anyway (i.e. assigning an attribute etc) to instruct the server to remember a variables state *without* having to go through the rigmarole of saving and loading to and from the Session state manually or similar workaround for any Types (including custom types) in exactly the same way web controls...
1
1457
by: Hugo Flores | last post by:
Hi, I'm trying to pass information between pages. I'm trying to avoid using QueryString and Session variables. I saw somewhere that I could use contxt.Items.Add (combined with Server.Transfer) to store my information and send it to another page. Some of my pages, either go to other pages, or postback depending on the situation. When I began to get confused was when I saw that I needed to store my variables on the viewstate if I wanted...
1
2772
by: cheloman12 | last post by:
Hi, I posted this problem some days ago. I repeat it because it was no answered. If description is confused, please make me know and i'll try to explain the case better. Thanks a lot, Marcelo "I’m implementing SavePageStateToPersistenceMedium and LoadPageStateFromPersistenceMedium in asp.net 2.0 to increment performance
2
246
by: digbydog | last post by:
Hello, I am a bit confused with viewstate - am trying to use viewstate to hold a boolean value which is false initially and then gets set to true after a certain event. I had the following Class ....
0
9700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10292
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9121
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7603
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.