Solved

asp.net page state

Posted on 2011-09-08
13
323 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:contel
13 Comments
 
LVL 5

Expert Comment

by:eridanix
ID: 36503556
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
 
LVL 7

Expert Comment

by:Gewgala
ID: 36503599
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
 
LVL 11

Expert Comment

by:b_levitt
ID: 36505220
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
 

Author Comment

by:contel
ID: 36899200
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
 
LVL 11

Expert Comment

by:b_levitt
ID: 36917156
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:contel
ID: 36917208
hi b_levitt,

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

thank you,
contel
0
 
LVL 11

Accepted Solution

by:
b_levitt earned 500 total points
ID: 36917600
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
 

Author Comment

by:contel
ID: 36922906
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
 
LVL 11

Expert Comment

by:b_levitt
ID: 36926598
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
 

Author Comment

by:contel
ID: 37056969
I've requested that this question be deleted for the following reason:

i didn't get any good solution...
0
 
LVL 11

Expert Comment

by:b_levitt
ID: 37056970
Contel, my solution provides you with the exact functionality you request in your original question.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now