ASP.NET Developer Notes

Ryan Garaygay's ASP.NET notes online

Page ViewState, Control ViewState and ControlState

Just a quick note. I was working on some Web User Control and needed to persist information across postbacks and thought of using ViewState. But while I was working on it, I realized that I was using the same key for saving another information in the parent Page's (the Page containing the user control) ViewState.

I did a little verification and it turns out that despite having the same key being used for saving different values to ViewState, having it assigned inside the Web User Conrol and inside the Page results to two different ViewState items. I verified by assigning ViewState to the Page's and the User Control's Page_Load event handler, then since the user control's page load fires later than the two, placed a breakpoint after it, checked the ViewState (while inside the User Control code) and it returned the value assigned inside it, while calling Page.ViewState and the same key, it retrieved the value that was previously assigned from the Page_Load of the parent Page. 

This is the behavior as far as my little test has shown but feel free to verify in your machine and let us know how it goes especially if the behavior is different from above. Haven't come across an explanation yet but will update this as soon as I can or maybe could stumble upon and comment. :) Nevertheless just FYI.

This is not to be confused with Control State which is discussed more clearly from the links below:

Control state in ASP.NET 2.0 by Fritz Onion (PluralSight)

Control State vs. View State Example on MSDN

ControlState Property Demystified 

Cross post from : .NET Developer Notes on Page ViewState, Control ViewState and ControlState

Comments

Sen Gupta said:

Yes, you are rigth. We also found out the same, and moved the item to session.

# June 27, 2010 1:05 PM

http://google.com said:

Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You clearly know what youre talking about, why throw away your intelligence on just posting videos to your site when you could be giving us something enlightening to read?

# April 1, 2013 9:37 PM