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
contelAsked:
Who is Participating?
 
b_levittConnect With a Mentor Commented:
I decided this would be easier with an example:
<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script runat="server">
      protected void Page_Load(object sender, EventArgs e)
      {
        OptMessages.Text = "";
        WireControlsForChange(this);
        
      }
      protected void Input_Changed(object sender, EventArgs e)
      {
        OptMessages.Text += ((Control)sender).ID + " was changed.\r\n";
      }

      protected void IptGo_Click(object sender, EventArgs e)
      {
        OptMessages.Text += ((Control)sender).ID + " was clicked.\r\n";
      }

      protected void WireControlsForChange(Control container)
      {
        foreach (Control control in container.Controls)
        {
          //if this control is a container control...
          if (control.HasControls())
          {
            //...then recurse...
            WireControlsForChange(control);
          }
          else //wire up applicable controls
          {
            if (control is IEditableTextControl)
              ((IEditableTextControl)control).TextChanged += new EventHandler(Input_Changed);
            else if (control is CheckBox)
              ((CheckBox)control).CheckedChanged += new EventHandler(Input_Changed);
          }
          
        }
      }
      
</script>
</head>
<body>
    <form id="form1" runat="server">
    
    <div>
      IptTextBox1: <asp:TextBox ID="IptTextBox1" runat="server" ></asp:TextBox>
      <br />
      IptTextBox2: <asp:TextBox ID="IptTextBox2" runat="server" ></asp:TextBox>
      
      <br />
      IptDDL1:<asp:DropDownList ID="IptDDL1" runat="server">
        <asp:ListItem>Item 1</asp:ListItem>
        <asp:ListItem>Item 2</asp:ListItem>
        <asp:ListItem>Item 3</asp:ListItem>
      </asp:DropDownList>
      
      <br />
      IptCheckbox1:<asp:CheckBox ID="IptCheckBox1" runat="server"/>
      <br />
      <asp:Button ID="IptGo" runat="server" Text="Go (PostBack)" 
        onclick="IptGo_Click" />
      <br />
      
    </div>
    <asp:TextBox ID="OptMessages" runat="server" TextMode="MultiLine" 
      Height="227px" Width="561px"></asp:TextBox>
    </form>
</body>
</html>

Open in new window

0
 
eridanixCommented:
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.
0
 
GewgalaCommented:
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.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
b_levittCommented:
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

0
 
contelAuthor Commented:
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
0
 
b_levittCommented:
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


0
 
contelAuthor Commented:
hi b_levitt,

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

thank you,
contel
0
 
contelAuthor Commented:
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
0
 
b_levittCommented:
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.
0
 
contelAuthor Commented:
I've requested that this question be deleted for the following reason:

i didn't get any good solution...
0
 
b_levittCommented:
Contel, my solution provides you with the exact functionality you request in your original question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.