ViewState VS Session

  • Thread starter Thread starter dotComPaJi
  • Start date Start date
D

dotComPaJi

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<Organization> orgs = Organization.GetAllOrganization();

orgs.Sort(UserManagementSupports.SortingOrganization);

ViewState["Organization"] = orgs;

GridView1.DataSource = ViewState["Organization"];

GridView1.DataBind();

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

orgs.Sort(UserManagementSupports.SortingOrganization);

Session["Organization"] = orgs;

GridView1.DataSource = Session["Organization"];

GridView1.DataBind();
 
Hi,

Read about the difference between StateBag and Session to hold values
between postbacks.
 
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).
 
Back
Top