473,387 Members | 1,464 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,387 software developers and data experts.

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(nothing is displayed in GridView but If I use Session State then it
works fine. Any idea???

-----------------------------------------------
//Does not work as expected
List<Organizationorgs = Organization.GetAllOrganization();

orgs.Sort(UserManagementSupports.SortingOrganizati on);

ViewState["Organization"] = orgs;

GridView1.DataSource = ViewState["Organization"];

GridView1.DataBind();

-----------------------------------------------
//Work perfectly
List<Organizationorgs = Organization.GetAllOrganization();

orgs.Sort(UserManagementSupports.SortingOrganizati on);

Session["Organization"] = orgs;

GridView1.DataSource = Session["Organization"];

GridView1.DataBind();

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

Thanks
DNB
Sep 5 '07 #1
2 3149
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.com

"dotComPaJi" <in**@t.comwrote in message
news:uR**************@TK2MSFTNGP03.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(nothing is displayed in GridView but If I use Session State then
it works fine. Any idea???

-----------------------------------------------
//Does not work as expected
List<Organizationorgs = Organization.GetAllOrganization();

orgs.Sort(UserManagementSupports.SortingOrganizati on);

ViewState["Organization"] = orgs;

GridView1.DataSource = ViewState["Organization"];

GridView1.DataBind();

-----------------------------------------------
//Work perfectly
List<Organizationorgs = Organization.GetAllOrganization();

orgs.Sort(UserManagementSupports.SortingOrganizati on);

Session["Organization"] = orgs;

GridView1.DataSource = Session["Organization"];

GridView1.DataBind();

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

Thanks
DNB

Sep 5 '07 #2
Hi,

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

"dotComPaJi" <in**@t.comwrote in message
news:uR**************@TK2MSFTNGP03.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(nothing is displayed in GridView but If I use Session State then
it works fine. Any idea???

-----------------------------------------------
//Does not work as expected
List<Organizationorgs = Organization.GetAllOrganization();

orgs.Sort(UserManagementSupports.SortingOrganizati on);

ViewState["Organization"] = orgs;

GridView1.DataSource = ViewState["Organization"];

GridView1.DataBind();

-----------------------------------------------
//Work perfectly
List<Organizationorgs = Organization.GetAllOrganization();

orgs.Sort(UserManagementSupports.SortingOrganizati on);

Session["Organization"] = orgs;

GridView1.DataSource = Session["Organization"];

GridView1.DataBind();

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

Thanks
DNB

Sep 5 '07 #3

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

Similar topics

10
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...
1
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...
1
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? ...
6
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...
3
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...
6
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
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...
6
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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...

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.