Are you asking "how do I pull from viewstate?" ViewState is a statebag
object, which has key/value pairs. To pull from viewstate, you can pull
directly from ViewState:
//direct pull
string value = (string) ViewState["keyName"];
The key here is a) pulling the correct value and b) pulling at the right
time (before it is reset). This is rather easy, as the order is (i.e., are
you asking "when can I pull from Viewstate?"):
Page.Init
Page.LoadViewState
Page.ProcessPostData
Page.Load
Change events for controls
Server side validation
Button.Click or Button.Command events
Page.PreRender
Page.SaveViewState <<< This is where it is too late to pull the old value
Page.Render
As long as you grab the old value before Page.SaveViewState, you are fine.
As a poor man's option, you can also save the value to ViewState as another
value:
ViewState["ControlCurrentValue"] = ControlName.Text;
You can then pull this and check. NOTE, however, that this is not wise for
all controls on a page, esp. a large page, as ViewState will get very large.
It is less of a problem in 2.0, but still a problem. This is necessary if you
are pulling the value at Render (see no reason to do this, of course).
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
***************************
Think Outside the Box!
***************************
"epigram" wrote:
I'm responding to a button click event on an asp.net web form. I then need
to retrieve the value from a TextBox control and I want to compare it
against the control's previous value to see if it has changed. How can I
retrieve a control's previous value from the ViewState? I know that I could
save the control's previous value in a session variable, reretrieve it's
value from the db, etc. But it would appear the control's previous value
would be in the ViewState since that is what ASP.NET uses to fire events
such as TextChanged.
Thanks!