Link to home
Start Free TrialLog in
Avatar of contel
contel

asked on

asp.net page state

hello,

how can i check if the page state has change ? there is quick way of doint this ?
for examle i hvae page with 3 TextBox and 1 Button, and on the button click i want to check if the texTBox Text was changed by the user. i don't want to compare each of their value with it's previously value (saving the previous value with ViewState or Session) becaue i want the solution also to fit to page with 15 text box and 10 dropdownList for example ?

thank you,
contel
Avatar of eridanix
eridanix
Flag of Czechia image

Hi,

I think the best aproach to solve this is to use javascript. When user change value in one or more of textboxes, than set the value of one hidden field to changed flag, which tells you if something was changed after send form to server.

The hidden feld should looks like:
<asp:HiddenField id="FormIsChanged" runat="server" value="0" />
and after change in some textbox set the value to 1 by javascript
<asp:HiddenField id="FormIsChanged" runat="server" value="1" />

On server side, you have to control only one control - a state of FormIsChanged hidden field value.
I think what you need is the StateBag.

From the following article:
http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx

"...In addition to storing values by key name, the StateBag has a TRACKING ability. Tracking is either on, or off. Tracking can be turned on by calling TrackViewState(), but once on, it cannot be turned off. When tracking is ON, and ONLY when tracking is ON, any changes to any of the StateBag's values will cause that item to be marked as "Dirty" ..."

There are some pretty good examples on how to use StateBag in that article.

You may also want to check out the StateBag entry on MSDN:
http://msdn.microsoft.com/en-us/library/system.web.ui.statebag.isitemdirty.aspx

Hope this helps.
Avatar of b_levitt
b_levitt

Most input controls have a "Changed" event that by default is fired upon a postback.  Therefore you could create a function that looped thru all the controls and wired up their changed event to a single handler (see below).

Your Control_Changed event handler could then do what ever you want, ie simply set a global "Changed" boolean, create a list of changed controls, etc.
 
If you are interested in this route, but are unsure how to implement, let me know and I'll write up an example.


- for each control within a container...
  - check the HasControls property of the control. if it's true... 
    - recursively call this function with the current control as the container
  - if the control is a textbox
    - attach the "Control_Changed" handler to the TextChanged event
  - if the control is a ddl
    -attach the "Control_Changed" handler to the SelectedIndexChanged event
  - if the control is a....

Open in new window

Avatar of contel

ASKER

hello,

eridanix - your solution isn't good because i am seeking for something generic, that will fit to page with textbox, dropdowns, radiobuttons and other control, and i don't want to go the all project and do the change for every page - it will take couple of months.

b_levitt - i don't want the change cause all the controls in the page to do postback.
i just want to seek when the user push the button if there are "dirty" control in the page or not,
also this solution is not generic because it is not fit to all controls ( if i insert listbox for example tommorw than i need to change the code )

Gewgala: i am trying to use stateBag with object and it didn't work - for example in OnLoad function i set the "key" of the stateBag to be textBox, than i operate function trackViewState on the stateBag.
than on the screen i push the the button ( without changing the textBox text) and on the click event check if the stateBug.Isdirty("key") - and i get True.

if you have more suggestions i will appreciate it...

thank you,
contel
My change does not force all controls to do a post back.  The changed event is only raised when there is an EVENTUAL postback (by a button for instance).  The only way a non-button type input does a postback on every change is if it's AutoPostBack property is set to true.

For example:
USER:
- edit textbox
- edit textbox 2
- click button (postback occurs here)

Server (on single postback from button)
- textbox raises changed event
- textbox 2 raises changed event
- button raises clicked event


Avatar of contel

ASKER

hi b_levitt,

i have some controls that i don't want them to do postback at all...

thank you,
contel
ASKER CERTIFIED SOLUTION
Avatar of b_levitt
b_levitt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of contel

ASKER

hi b_levitt,

it's won't work because you don't have autopostback=true on IptTextBox1 and IptTextBox2 and IptCheckBox1, so function  Input_Changed will never call....
and if you put autopostback=true on this controls than this is the problem i don't want all the controls to cause postback.

thank you,
contel
As stated earlier, autopostback is not a requirement of the input_changed event.  For autopostback=false controls, input_changed is delayed until the next postback.

Please paste my example into a new aspx page and run it.  It does exactly what your original question describes.
Avatar of contel

ASKER

I've requested that this question be deleted for the following reason:

i didn't get any good solution...
Contel, my solution provides you with the exact functionality you request in your original question.