Link to home
Start Free TrialLog in
Avatar of PerlChaser
PerlChaser

asked on

ViewState not saving between postbacks

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

Avatar of crazyman
crazyman
Flag of United Kingdom of Great Britain and Northern Ireland image

you have enableviewstate on your page?
ASKER CERTIFIED SOLUTION
Avatar of JunkMan
JunkMan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PerlChaser
PerlChaser

ASKER

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

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

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.
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?
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 https://www.experts-exchange.com/questions/23822065/Error-on-postback-after-adding-ViewState.html?anchorAnswerId=22739964#a22739964.
Testing that ViewState worked was what allowed me to reach a solution.  My solution is to trim down what I am saving in ViewState.