Solved

ViewState not saving between postbacks

Posted on 2008-10-16
8
5,715 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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