views:

1039

answers:

4

Here are the basics of my setup:

I have a BasePage class that inherits from System.Web.UI.Page. BasePage has two properties implemented such that their get/set does so from two different items in the ViewState collection (ex this.ViewState["Year"] from the BasePage).

I have another class that then inherits from BasePage, lets call it SpecificBasePage.

Finally, I have an aspx page that inherits SpecificBasePage.

I have added break points and done much stepping through my code and have found that on the initial page load, both of my view state properties are assigned values and the values persist throughout the first page load life cycle.

When the page is posted back however, during the Page_Load event and other event handlers (when the ViewState should be loaded), both properties return null. Inspecting this.ViewState.Count shows that there are zero objects in the collection.

Can anyone think of something I might be doing somewhere that would effect the ViewState and cause this behavior?

--Addition I have isolated it to a portion of my code. On the initial load, I give the viewstate properties values in OnInit, I have found that when I move this to OnLoad, the values persist across the post back. I guess even though the added view state values persist throughout the initial page lifecycle, they are abandoned in the post back?

+3  A: 

The pages might have EnableViewState = false on the aspx

Also might be disabled on the web.config of the application, or even for the overall computer on the element .

Update 1: On the asp.net lifecycle, ViewState is loaded after Init, and before Load more info. See it like anything on init is considered part of the declaration of the page. Later asp.net will load the viewstate, and after that it starts tracking changes to the viewstate. It is after this that asp.net will see any difference between the initial information on viewstate and the changed information, so moving it to load avoids the problem (as initial state is not there, and new state is whatever you put on the viewstate). From the link above:

"The reason is because the StateBag class only tracks changes to its members after its TrackViewState() method has been invoked. That is, if you have a StateBag, any and all additions or modifications that are made before TrackViewState() is made will not be saved when the SaveViewState() method is invoked. The TrackViewState() method is called at the end of the initialization stage, which happens after the instantiation stage. Therefore, the initial property assignments in the instantiation stage—while written to the ViewState in the properties' set accessors—are not persisted during the SaveViewState() method call in the save view state stage, because the TrackViewState() method has yet to be invoked."

eglasius
Thanks for the reply. I did a project wide search for EnableViewState, and I had in in a few controls, but none on this page, and none in the web.config
spilliton
A: 

As I stated in the part I added later, apparently you can't set anything in the view state before page load, EVEN THOUGH Asp.net doesn't throw an error and it will exist in the view state throughout the initial page life cycle, it will NOT persist on the subsequent post back's life cycle.

spilliton
A: 

Take a look at where you are setting you ViewState values. Possibly wireup your OnUnload event and inspect the ViewState object just to make sure the values are actually getting set.

Andrew Robinson
+2  A: 

Here's an excellent answer another user posted about ASP.NET page lifecycle it's worth a look for ViewState issues.

Also, have a look at "Truly Understanding ViewState"

Gavin Miller