Solved

ViewState not saving between postbacks

Posted on 2008-10-16
8
5,767 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

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