Viewstate

In answer to my rant about Viewstate, Jesse wrote :

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.

OK Jesse, but what's happen after a postback ? If I need to keep the value? And why can I read the value of the Control viewstate in debug mode, and not from my code?

So far, the best solution has to be an hidden input box, and it works very well.

Until I can read somewhere a clear article on how to use Viewstate properly between controls

1 Comment

  • "which gets and sets the value inside of the viewstate"


    that's the key bit of what Jesse wrote. the public property is merely a window into the viewstate, so it'll persist betwen page loads.


    Think about it. Its not that hard!

Comments have been disabled for this content.