How do I know when the user makes changes on a web form?

V

Vinay

Hi All:

I have a web form that is displaying some data. What I want to do is:

- Set a flag when the user modifies any data on the form
- Ask the user if the changes are to be discarded when the user hits the exit button
- If user says Yes/OK I continue and quit the screen
- If user says Cancel I stop and return to the screen.

Here is what I have tried:

In the page_load event I have added the following code:

cmdCancel.Attributes.Add("onclick", "return confirm('Quit without saving?')")

But this causes it to fire even if there is no change - I (my client) want this to fire only if there is a change.

So I tried to add the same line in the _TextChanged event of a textbox control. The idea being once the user saves the form contents by hitting 'Save' I will use cmdCancel.Attributes.Remove("onclick") and add it back when _TextChanged detects a change.

This is not working right - I guess the way I have tried to implement it is flawed.

Is there anything other way I can do this?

Any help would be much appreciated.

TIA!

Vinay
(e-mail address removed)
 
J

John Saunders

Hi All:

I have a web form that is displaying some data. What I want to do is:

- Set a flag when the user modifies any data on the form
- Ask the user if the changes are to be discarded when the user hits the exit button
- If user says Yes/OK I continue and quit the screen
- If user says Cancel I stop and return to the screen.

Here is what I have tried:

In the page_load event I have added the following code:

cmdCancel.Attributes.Add("onclick", "return confirm('Quit without saving?')")

But this causes it to fire even if there is no change - I (my client) want this to fire only if there is a change.

So I tried to add the same line in the _TextChanged event of a textbox control. The idea being once the user saves the form contents by hitting 'Save' I will use cmdCancel.Attributes.Remove("onclick") and add it back when _TextChanged detects a change.

This is not working right - I guess the way I have tried to implement it is flawed.

Is there anything other way I can do this?
You'll have to do this client-side, with JavaScript.
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top