How to retrieve control's value from ViewState

E

epigram

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!
 
G

Guest

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!
***************************
 
E

epigram

I was asking the first question, although the answer to the second one is
key! Thanks. The thing I am trying to do is to get to the actual control
that is stored in the ViewState (or it's text value), not an object that I
placed into the ViewState statebag. I tried doing this, in my button click
event handler, with code such as:

TextBox tbxSaved = (TextBox)ViewState["myTextBoxId"];

where myTextBoxId is the value of the id attribute of the TextBox control on
the form. It comes back as <undefined value>. Any ideas what I am doing
wrong or if this is possible?

Thanks.


Cowboy (Gregory A. Beamer) - MVP said:
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 said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top