Viewstate (2)

"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"

[Paschal]

After postback, the values will be there still, unless ViewState is explicitly disabled by the user. This is the way all the .NET WebControls work (try setting the text of a control in onload when !IsPostBack and notice how it will retain its value for the lifetime of the page, even though you never explicitly set it again).

You can read the value of viewstate from debug mode probably because it is using Reflection to examine the class. Reflection allows you to read both protected and private class members when the appropriate BindingFlags are set.

I don't know of any article that explains using viewstate between controls other than what I have already said about using a property that gets and sets a viewstate member. That is how you should always do it. You should never access the viewstate directly.

No Comments