Solved

ViewState not saving between postbacks

Posted on 2008-10-16
8
5,635 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
imap read mail 1 28
Search an image for an image 3 30
Store results in vb.net 3 20
transaction in asp.net, sql server 6 33
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

778 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