ViewState

"Manipulating Viewstate on a page is quite easy, using a property to save a state with a pair of get and set, fine..."

Page.findcontrol("myctl").viewstate("Myvalue")

"And why I can't use this sentence in my code ? Because the compiler will produce an error saying that the Viewstate of the page is Protected and not public."

"What the hell does it mean ?? The only reliable solution I found for the moment is to go back to the old trick of using hidden input box."

"...So why .Net guys claims everywhere that Viewstate is there to replace our old habit to use hidden fields if it's not true. And so , what is the solution to pass values from one control to another using Viewstate ?"
[Paschal]

Your answer is in the first part of your post. You create a public property which gets and sets the value inside of the viewstate:

((ControlType)Page.FindControl("myctl")).MyValue = "something";

Even if you could access viewstate directly, this would be a better solution, because now everyone gets autocomplete on your property as well as compile time type checking. The error you were getting means that you can't access the viewstate property because it is marked as "protected." Protected members of classes can only be from within the class itself or within members of classes derived from the class that declared the protected member.

No Comments