Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect User Changes

Status
Not open for further replies.

bjd4jc

Programmer
Nov 8, 2001
1,627
US
I have a form that is stored and retrieved from a database. I would like to be able to check if a user has changed the form before they go to another page or close the browser. What is the best way to go about this in ASP.NET?
 
Not entirely sure why you need to check if changes have been made... But, here's something along the lines of what I think I'd do.

Let's say you have a user class with a first name and a last name (I'm using public properties just for brevity's sake:

Code:
public class User
{
    public string FirstName;
    public string LastName;

    public User()
    {
        this.FirstName = String.Empty;
        this.LastName  = String.Empty;
    }
}

When your form page loads, I would then populate the properties from the database:

Code:
/* Class-scope variables */
private User m_oUser;

public void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
    /*...[sql query stuff here]...*/
    m_oUser = new User();
    m_oUser.FirstName = (string)[DataView][0]["FirstNameColumn"];
    m_oUser.LastName = (string)[DataView][0]["LastNameColumn"];
}
}

Then, whenever you need to check for changes on the page, you could create a function like this (let's say there are two textboxes on the form - txt_firstName and txt_lastName):

Code:
private bool HasChanged()
{
    User _oUser = new User();
    _oUser.FirstName = txt_firstName.Text;
    _oUser.LastName = txt_lastName.Text;

    return _oUser.Equals(this.user);
}

This will allow you to compare the objects and handle them as needed on demand without multiple database hits. It's also more along the lines of being the "proper" OOD way of doing things. There are, of course, better ways to do this, but for the sake of brevity...

To get the functionality that you want, you would need to put this stuff into pretty much any link that you have on the page, as well as figure out a way to trigger the event on page onClose() event. (Check out using the Page.GetPostBackClientEvent() method - you may be able to manipulate it to do what you want, but I'm not completely sure).

Hope this helps

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Thanks! I am going to try some of the code out. I won't get to it for a couple of days though. I'll come back here and post and let you know how it went.

BTW- the reason I want to check for changes is to prompt the user to save them before they go somewhere else.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top