retrive control from ViewState

E

epigram

I've tried asking this question several times and have received many good
answers, but ones that don't quite answer my question. This leads me to
believe that I am trying to do something very awkward or I don't know how to
pose the question correctly. Let me try again. I appreciate any input!
:)

I have an asp.net web form with a TextBox control and a submit button. The
TextBox control's text property is set on the server before it is first
displayed to the user. When the form is posted back, because of the user
clicking the submit button, I am trying to determine what the value of the
TextBox control's text property was originally set to so that I can manually
compare it to what the text property is currently set to. I know I can
setup an event handler to capture the TextBox's text change event, but I
don't want to do that. I thought I could obtain the TextBox control out of
the ViewState. 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>.

If you want the rest of the story here I've actually got several TextBox
controls on my form. Depending on which ones change I will have to execute
different db updates. It seems odd to respond to text change events for all
these controls and maybe set some dirty flags for each control and determine
later (in some other asp.net page event I suppose) which controls had
changed and determine my course of action. What I want to do is in the
button click event handler manually determine which controls have changed
and determine my course of action all from within this handler. The need I
have seems very routine, so clearly there is a easy way to handle this. It
just currently escapes me.

Any help would be much appreciated! Thanks!
 
B

Bruce Barker

you have to capture the original text from viewstate before LoadPostData
event fires. when the postback data method is called by the framework the
text value (and viewstate) are updated to the new value.

note: when the framework calls a LoadPostData, the control returns whether
to fire the on changed event. the framework uses this return value, to know
whether to fire the onchanged event (the control does not track this). for
controls whose LoadPostData returns true, it later calls their
RaisePostDataChanedEvent, this method then actually fires the onchanged
event.

-- bruce (sqlwork.com)
 
E

epigram

What is the best practice for trying to deal with the particular scenario I
have described below?


Bruce Barker said:
you have to capture the original text from viewstate before LoadPostData
event fires. when the postback data method is called by the framework the
text value (and viewstate) are updated to the new value.

note: when the framework calls a LoadPostData, the control returns whether
to fire the on changed event. the framework uses this return value, to
know whether to fire the onchanged event (the control does not track
this). for controls whose LoadPostData returns true, it later calls their
RaisePostDataChanedEvent, this method then actually fires the onchanged
event.

-- bruce (sqlwork.com)


epigram said:
I've tried asking this question several times and have received many good
answers, but ones that don't quite answer my question. This leads me to
believe that I am trying to do something very awkward or I don't know how
to pose the question correctly. Let me try again. I appreciate any
input! :)

I have an asp.net web form with a TextBox control and a submit button.
The TextBox control's text property is set on the server before it is
first displayed to the user. When the form is posted back, because of
the user clicking the submit button, I am trying to determine what the
value of the TextBox control's text property was originally set to so
that I can manually compare it to what the text property is currently set
to. I know I can setup an event handler to capture the TextBox's text
change event, but I don't want to do that. I thought I could obtain the
TextBox control out of the ViewState. 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>.

If you want the rest of the story here I've actually got several TextBox
controls on my form. Depending on which ones change I will have to
execute different db updates. It seems odd to respond to text change
events for all these controls and maybe set some dirty flags for each
control and determine later (in some other asp.net page event I suppose)
which controls had changed and determine my course of action. What I
want to do is in the button click event handler manually determine which
controls have changed and determine my course of action all from within
this handler. The need I have seems very routine, so clearly there is a
easy way to handle this. It just currently escapes me.

Any help would be much appreciated! Thanks!
 
H

hashimisayed

epigram,

LoadPostData is the correct way to do it, but the easiest way is to
just throw another entry into the viewstate to hold the old value. For
example, when you populate the textbox.Text property on the server,
just add another entry to the ViewState called OLD_VALUE. Upon
postback, retrive OLD_VALUE and compare it to textbox.Text.

Not the absolute best solution, but a very easy practicle one.

sayed
 

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

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top