473,664 Members | 3,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ViewState VS Session

Hello There,

I have a simple question.

In one of my program if I use ViewState then program does not work as
expected(nothin g is displayed in GridView but If I use Session State then it
works fine. Any idea???

-----------------------------------------------
//Does not work as expected
List<Organizati onorgs = Organization.Ge tAllOrganizatio n();

orgs.Sort(UserM anagementSuppor ts.SortingOrgan ization);

ViewState["Organizati on"] = orgs;

GridView1.DataS ource = ViewState["Organizati on"];

GridView1.DataB ind();

-----------------------------------------------
//Work perfectly
List<Organizati onorgs = Organization.Ge tAllOrganizatio n();

orgs.Sort(UserM anagementSuppor ts.SortingOrgan ization);

Session["Organizati on"] = orgs;

GridView1.DataS ource = Session["Organizati on"];

GridView1.DataB ind();

-----------------------------------------------

Thanks
DNB
Sep 5 '07 #1
2 3159
The context of the viewstate is across a single page. For example, if
you have a page, and you store the state in the viewstate, then when the
page is posted back to, the viewstate should be populated with whatever you
put in it when the page was originally sent to the user.

The session state is used for storing information for the session across
multiple pages.

They have different scopes. If you are going to use the viewstate, then
you should initialize it once, and then pass it back and forth between
client and server. For what you are doing, it seems that the session is a
better place to place the list, or, initialize the viewstate once, when
looking at the page for the first time (not posting back).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"dotComPaJi " <in**@t.comwrot e in message
news:uR******** ******@TK2MSFTN GP03.phx.gbl...
Hello There,

I have a simple question.

In one of my program if I use ViewState then program does not work as
expected(nothin g is displayed in GridView but If I use Session State then
it works fine. Any idea???

-----------------------------------------------
//Does not work as expected
List<Organizati onorgs = Organization.Ge tAllOrganizatio n();

orgs.Sort(UserM anagementSuppor ts.SortingOrgan ization);

ViewState["Organizati on"] = orgs;

GridView1.DataS ource = ViewState["Organizati on"];

GridView1.DataB ind();

-----------------------------------------------
//Work perfectly
List<Organizati onorgs = Organization.Ge tAllOrganizatio n();

orgs.Sort(UserM anagementSuppor ts.SortingOrgan ization);

Session["Organizati on"] = orgs;

GridView1.DataS ource = Session["Organizati on"];

GridView1.DataB ind();

-----------------------------------------------

Thanks
DNB

Sep 5 '07 #2
Hi,

Read about the difference between StateBag and Session to hold values
between postbacks.

"dotComPaJi " <in**@t.comwrot e in message
news:uR******** ******@TK2MSFTN GP03.phx.gbl...
Hello There,

I have a simple question.

In one of my program if I use ViewState then program does not work as
expected(nothin g is displayed in GridView but If I use Session State then
it works fine. Any idea???

-----------------------------------------------
//Does not work as expected
List<Organizati onorgs = Organization.Ge tAllOrganizatio n();

orgs.Sort(UserM anagementSuppor ts.SortingOrgan ization);

ViewState["Organizati on"] = orgs;

GridView1.DataS ource = ViewState["Organizati on"];

GridView1.DataB ind();

-----------------------------------------------
//Work perfectly
List<Organizati onorgs = Organization.Ge tAllOrganizatio n();

orgs.Sort(UserM anagementSuppor ts.SortingOrgan ization);

Session["Organizati on"] = orgs;

GridView1.DataS ource = Session["Organizati on"];

GridView1.DataB ind();

-----------------------------------------------

Thanks
DNB

Sep 5 '07 #3

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

Similar topics

10
2260
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 ?
1
3745
by: Ralph Soons | last post by:
Hi all, I am trying to save the viewstate in a session instead of storing it in a hidden of the webpage which is default. This because of performance reasons. When I use line 2 in combination with line 4, my application works. Using Line 1 in combination with line 3 (and of course renaming httpSessionState1 to httpSessionState) results in the error as mentioned below. protected override object LoadPageStateFromPersistenceMedium() {
1
1703
by: Sky | last post by:
Although I've been using C# for the last month or so, and accepting out of blind faith the ViewState, I do have some nagging questions about it... can you help verify the following statements? Application is across all sessions. Session is across all pages. ViewState is sort of like Session, but for only one page. <-- Correct statement? Session is stored between page requests, somewhere in mem or IIS files --
6
4340
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
2076
by: MattC | last post by:
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();
6
1605
by: Max | last post by:
I need an vb.net class that is invoked from aspx page, that use the viewstate/session object. This class must be store the information into viewstate/session. Can you give me an example ? Thanks
3
4561
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...
6
1589
by: mosscliffe | last post by:
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(ViewState("PbCounter")) Then
0
8437
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
8778
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
8549
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
7375
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
6187
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
5660
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4185
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...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.