?
Solved

ViewState not saving between postbacks

Posted on 2008-10-16
8
Medium Priority
?
5,836 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 200 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

752 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