By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,870 Members | 1,236 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,870 IT Pros & Developers. It's quick & easy.

Checking if form values have changed between postbacks

P: n/a
Hi,

I have an aspx page with a number of web controls on it and one of these is
a cancel button. I want to check the page to see if the user has changed any
of the controls, i..e typed some text in a textbox, changed a dropdown etc

The reason I want to do this is when the user clicks cancel I want to notify
them that their changes will be lost using a popup or something.

My question is what is the best way to compare the page that was presented
to the user originally against the one they're cancelling? I'm thinking of
getting the hashcode of the viewstate or something like this....probably on
the wrong lines though.

any help appreciated.

thks
Nov 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Tomas,

Thanks for that but it's not exactly what I want, probably didn't explain it
well enough.

I only want to show this pop up if the user has changed some values, so what
I really want is a mechanism to know if the user has changed anything, if
they have I show the popup otherwise I don't.

"Tomas" wrote:
Hi Neil,

One very simple answer to this would be to use the <input type="reset"
value="Cancel">

This will automatically reset all the controls on the form to their original
values. If you want a pop up, just use the onclick event.

<input type="reset" onclick="return prompt('All your data will be lost.
Continue?');" value="Cancel">

That is the simpliest way I know. Let me know if that helps,

Tom

"Neil" wrote:
Hi,

I have an aspx page with a number of web controls on it and one of these is
a cancel button. I want to check the page to see if the user has changed any
of the controls, i..e typed some text in a textbox, changed a dropdown etc

The reason I want to do this is when the user clicks cancel I want to notify
them that their changes will be lost using a popup or something.

My question is what is the best way to compare the page that was presented
to the user originally against the one they're cancelling? I'm thinking of
getting the hashcode of the viewstate or something like this....probably on
the wrong lines though.

any help appreciated.

thks

Nov 18 '05 #2

P: n/a
its more of javascript code than asp.net code
1. create a global javascript variable.. within script tag without being in
function
2. for all you webcontrols as a javascript onChange etc which will modify
the value of the variable created in 1.
3. the reset button will have a noclick set to another function that can
check the value of the variable and determine if it was modified.
4. if it was modified say something like

var modified = false // at the start on modification call

function SetModified()
{
modified = true;
}

function CheckForModification()
{
if(modified == true)
{
alert("you modified.. you sure you want to cancel ?");
return true;
}
else
{
return false;
}
}

This is all javascript based code. I do not recommend this method as you
will lose your ability to use validator controls etc. But again you might
not want to use them.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Neil" <Ne**@discussions.microsoft.com> wrote in message
news:60**********************************@microsof t.com...
Hi Tomas,

Thanks for that but it's not exactly what I want, probably didn't explain
it
well enough.

I only want to show this pop up if the user has changed some values, so
what
I really want is a mechanism to know if the user has changed anything, if
they have I show the popup otherwise I don't.

"Tomas" wrote:
Hi Neil,

One very simple answer to this would be to use the <input type="reset"
value="Cancel">

This will automatically reset all the controls on the form to their
original
values. If you want a pop up, just use the onclick event.

<input type="reset" onclick="return prompt('All your data will be lost.
Continue?');" value="Cancel">

That is the simpliest way I know. Let me know if that helps,

Tom

"Neil" wrote:
> Hi,
>
> I have an aspx page with a number of web controls on it and one of
> these is
> a cancel button. I want to check the page to see if the user has
> changed any
> of the controls, i..e typed some text in a textbox, changed a dropdown
> etc
>
> The reason I want to do this is when the user clicks cancel I want to
> notify
> them that their changes will be lost using a popup or something.
>
> My question is what is the best way to compare the page that was
> presented
> to the user originally against the one they're cancelling? I'm thinking
> of
> getting the hashcode of the viewstate or something like
> this....probably on
> the wrong lines though.
>
> any help appreciated.
>
> thks

Nov 18 '05 #3

P: n/a
Hi Dave,

Yeh I was trying to avoid doing this in javascript... I was hoping there
would be a more generic way also that would apply to the whole page, so if
new controls are added I don't need to assign client side handlers to them.

I was thinking of comparing request and response objects or viewstate or
something....

"Hermit Dave" wrote:
its more of javascript code than asp.net code
1. create a global javascript variable.. within script tag without being in
function
2. for all you webcontrols as a javascript onChange etc which will modify
the value of the variable created in 1.
3. the reset button will have a noclick set to another function that can
check the value of the variable and determine if it was modified.
4. if it was modified say something like

var modified = false // at the start on modification call

function SetModified()
{
modified = true;
}

function CheckForModification()
{
if(modified == true)
{
alert("you modified.. you sure you want to cancel ?");
return true;
}
else
{
return false;
}
}

This is all javascript based code. I do not recommend this method as you
will lose your ability to use validator controls etc. But again you might
not want to use them.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Neil" <Ne**@discussions.microsoft.com> wrote in message
news:60**********************************@microsof t.com...
Hi Tomas,

Thanks for that but it's not exactly what I want, probably didn't explain
it
well enough.

I only want to show this pop up if the user has changed some values, so
what
I really want is a mechanism to know if the user has changed anything, if
they have I show the popup otherwise I don't.

"Tomas" wrote:
Hi Neil,

One very simple answer to this would be to use the <input type="reset"
value="Cancel">

This will automatically reset all the controls on the form to their
original
values. If you want a pop up, just use the onclick event.

<input type="reset" onclick="return prompt('All your data will be lost.
Continue?');" value="Cancel">

That is the simpliest way I know. Let me know if that helps,

Tom

"Neil" wrote:

> Hi,
>
> I have an aspx page with a number of web controls on it and one of
> these is
> a cancel button. I want to check the page to see if the user has
> changed any
> of the controls, i..e typed some text in a textbox, changed a dropdown
> etc
>
> The reason I want to do this is when the user clicks cancel I want to
> notify
> them that their changes will be lost using a popup or something.
>
> My question is what is the best way to compare the page that was
> presented
> to the user originally against the one they're cancelling? I'm thinking
> of
> getting the hashcode of the viewstate or something like
> this....probably on
> the wrong lines though.
>
> any help appreciated.
>
> thks


Nov 18 '05 #4

P: n/a

"Tomas" <To***@discussions.microsoft.com> wrote in message news:06**********************************@microsof t.com...
Hi Neil,

One very simple answer to this would be to use the <input type="reset"
value="Cancel">

This will automatically reset all the controls on the form to their original
values. If you want a pop up, just use the onclick event.


Just a warning when you use the "reset" function mixed with postbacks:

the "original" value is the value that is written in the html, the *last time*
the page was received.
So: say you start with an empty form, the user adds some values and causes
a postback. After this a "reset" will not result in an empty form (as it would
have done before that postback), but in a form with values as they were
right after that postback.

Hans Kesting
Nov 18 '05 #5

P: n/a
Neil,

you can do that sort of a check on the server side. I misunderstood you
thinking you were looking to avoid roundtrip and even the notification if
there was no user activity other than click reset

what you can do is simple
either fetching the data again on user submission to cross check
or
store the values in a accessible location like for example writing to a
viewstate or session object or even a cache object

so now you have two copies of the data. The controls would reflect the state
as submited by the user. modified or not.
now you can do a parameter wise comparision.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Neil" <Ne**@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
Hi Dave,

Yeh I was trying to avoid doing this in javascript... I was hoping there
would be a more generic way also that would apply to the whole page, so if
new controls are added I don't need to assign client side handlers to them.
I was thinking of comparing request and response objects or viewstate or
something....

"Hermit Dave" wrote:
its more of javascript code than asp.net code
1. create a global javascript variable.. within script tag without being in function
2. for all you webcontrols as a javascript onChange etc which will modify the value of the variable created in 1.
3. the reset button will have a noclick set to another function that can
check the value of the variable and determine if it was modified.
4. if it was modified say something like

var modified = false // at the start on modification call

function SetModified()
{
modified = true;
}

function CheckForModification()
{
if(modified == true)
{
alert("you modified.. you sure you want to cancel ?");
return true;
}
else
{
return false;
}
}

This is all javascript based code. I do not recommend this method as you
will lose your ability to use validator controls etc. But again you might not want to use them.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Neil" <Ne**@discussions.microsoft.com> wrote in message
news:60**********************************@microsof t.com...
Hi Tomas,

Thanks for that but it's not exactly what I want, probably didn't explain it
well enough.

I only want to show this pop up if the user has changed some values, so what
I really want is a mechanism to know if the user has changed anything, if they have I show the popup otherwise I don't.

"Tomas" wrote:

> Hi Neil,
>
> One very simple answer to this would be to use the <input type="reset"> value="Cancel">
>
> This will automatically reset all the controls on the form to their
> original
> values. If you want a pop up, just use the onclick event.
>
> <input type="reset" onclick="return prompt('All your data will be lost.> Continue?');" value="Cancel">
>
> That is the simpliest way I know. Let me know if that helps,
>
> Tom
>
> "Neil" wrote:
>
> > Hi,
> >
> > I have an aspx page with a number of web controls on it and one of
> > these is
> > a cancel button. I want to check the page to see if the user has
> > changed any
> > of the controls, i..e typed some text in a textbox, changed a dropdown> > etc
> >
> > The reason I want to do this is when the user clicks cancel I want to> > notify
> > them that their changes will be lost using a popup or something.
> >
> > My question is what is the best way to compare the page that was
> > presented
> > to the user originally against the one they're cancelling? I'm thinking> > of
> > getting the hashcode of the viewstate or something like
> > this....probably on
> > the wrong lines though.
> >
> > any help appreciated.
> >
> > thks


Nov 18 '05 #6

P: n/a
Hi Dave,

Yeh I should have been clearer, its not a reset button but a link to return
to the previous page, so I wanted a way to notify the user that changes would
be lost if they didn't click save.

I think I'll probably end up doing a comparison between the original fields
and the ones on postback.

What I really hoped was for a very generic mechanism, like checking a hash
value of the original response against the postback or something, this way if
new controls are added etc I don't need to worry about editing the comparison
logic plus it could be used accross many pages.... maybe I want to much hey.

thanks for your help

"Hermit Dave" wrote:
Neil,

you can do that sort of a check on the server side. I misunderstood you
thinking you were looking to avoid roundtrip and even the notification if
there was no user activity other than click reset

what you can do is simple
either fetching the data again on user submission to cross check
or
store the values in a accessible location like for example writing to a
viewstate or session object or even a cache object

so now you have two copies of the data. The controls would reflect the state
as submited by the user. modified or not.
now you can do a parameter wise comparision.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Neil" <Ne**@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
Hi Dave,

Yeh I was trying to avoid doing this in javascript... I was hoping there
would be a more generic way also that would apply to the whole page, so if
new controls are added I don't need to assign client side handlers to

them.

I was thinking of comparing request and response objects or viewstate or
something....

"Hermit Dave" wrote:
its more of javascript code than asp.net code
1. create a global javascript variable.. within script tag without being in function
2. for all you webcontrols as a javascript onChange etc which will modify the value of the variable created in 1.
3. the reset button will have a noclick set to another function that can
check the value of the variable and determine if it was modified.
4. if it was modified say something like

var modified = false // at the start on modification call

function SetModified()
{
modified = true;
}

function CheckForModification()
{
if(modified == true)
{
alert("you modified.. you sure you want to cancel ?");
return true;
}
else
{
return false;
}
}

This is all javascript based code. I do not recommend this method as you
will lose your ability to use validator controls etc. But again you might not want to use them.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Neil" <Ne**@discussions.microsoft.com> wrote in message
news:60**********************************@microsof t.com...
> Hi Tomas,
>
> Thanks for that but it's not exactly what I want, probably didn't explain > it
> well enough.
>
> I only want to show this pop up if the user has changed some values, so > what
> I really want is a mechanism to know if the user has changed anything, if > they have I show the popup otherwise I don't.
>
> "Tomas" wrote:
>
>> Hi Neil,
>>
>> One very simple answer to this would be to use the <input type="reset" >> value="Cancel">
>>
>> This will automatically reset all the controls on the form to their
>> original
>> values. If you want a pop up, just use the onclick event.
>>
>> <input type="reset" onclick="return prompt('All your data will be lost. >> Continue?');" value="Cancel">
>>
>> That is the simpliest way I know. Let me know if that helps,
>>
>> Tom
>>
>> "Neil" wrote:
>>
>> > Hi,
>> >
>> > I have an aspx page with a number of web controls on it and one of
>> > these is
>> > a cancel button. I want to check the page to see if the user has
>> > changed any
>> > of the controls, i..e typed some text in a textbox, changed a dropdown >> > etc
>> >
>> > The reason I want to do this is when the user clicks cancel I want to >> > notify
>> > them that their changes will be lost using a popup or something.
>> >
>> > My question is what is the best way to compare the page that was
>> > presented
>> > to the user originally against the one they're cancelling? I'm thinking >> > of
>> > getting the hashcode of the viewstate or something like
>> > this....probably on
>> > the wrong lines though.
>> >
>> > any help appreciated.
>> >
>> > thks


Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.