Solved

ViewState not saving between postbacks

Posted on 2008-10-16
8
5,560 Views
Last Modified: 2013-12-17
I save "CachedControls" to the ViewState and verify that it is saved during the first visit to the page, but on postbacks, the ViewState is null.  Why does my ViewState object not persist between postbacks?

PerlChaser
private List<IEnumerable> ControlData

{

	get 

	{

		List<IEnumerable> data =(List<IEnumerable>)ViewState["CachedControls"];

		if (data == null)

		{

			EditController editControl = new EditController(SearchType); 

			data = editControl.GetData();

			ViewState["CachedControls"] = data;

		}

 

		return data; 

	}

}

Open in new window

0
Comment
Question by:PerlChaser
  • 4
  • 3
8 Comments
 
LVL 13

Expert Comment

by:crazyman
ID: 22735897
you have enableviewstate on your page?
0
 
LVL 3

Accepted Solution

by:
JunkMan earned 50 total points
ID: 22735907
If you've got enableviewstate="true" on your page declaration "<%@ Page" then there should be no reason for the viewstate not to be present, unless your editControl.GetData(); is returning null.

To test if the viewstate is indeed working, set the ViewState on a IsPostBack and try and retrieve it on a postack. That should be enough to tell you if there is an issue with your viewstate.

0
 
LVL 1

Author Comment

by:PerlChaser
ID: 22735986
I set EnableViewState="true" on the page and checked that my method is not returning null.  Then, I tried to add a "temp" ViewState on postback.  On the next postback, it's null and needs to set the variable again.
		protected void Page_Init(object sender, EventArgs e)

		{

			if (!IsPostBack)

				RenderView();

			else

			{

				if (ViewState["temp"] == null)

					ViewState["temp"] = "something";

			}

		}

Open in new window

0
 
LVL 3

Expert Comment

by:JunkMan
ID: 22735995
what if you set it on the first load, is it being set with something on postback anyway?
                protected void Page_Init(object sender, EventArgs e)

                {

                        if (!IsPostBack){

                                RenderView();

ViewState["temp"] = "not something";

}

                        else

                        {

                                if (ViewState["temp"] == null)

                                        ViewState["temp"] = "something";

                        }

                }

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:PerlChaser
ID: 22736041
Yes, that's exactly what happens when I insert the extra line.  The ViewState gets lost between posts.  I forgot to mention earlier that this is a modal dialog.
0
 
LVL 3

Expert Comment

by:JunkMan
ID: 22736131
oh hang on, i've just now realized, you're checking for viewstate way to soon in the page cycle, at Page_Init viewstate is still not loaded!... Obviously when it gets loaded "temp" will be overridden with nothing,

Back to the original issue, is it imperative that your ControlData is set on page_init?
0
 
LVL 1

Author Comment

by:PerlChaser
ID: 22740096
I don't need ControlData on Page_Init.  I just tried setting ViewState["temp"] in my Button_Click event, and it appears to be fine.  My problem appears to be that ControlData is fine until we leave the page, at which point it ceases to exist.  I suspect that this issue will be resolved with my other question at http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23822065.html#a22739964.
0
 
LVL 1

Author Closing Comment

by:PerlChaser
ID: 31506932
Testing that ViewState worked was what allowed me to reach a solution.  My solution is to trim down what I am saving in ViewState.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now