Confirming leaving the page.....

U

UJ

I've got a page where users can make changes (enable and disable some
checkboxes.) I need to have it so that if they have made changes and go away
from the page, they will get prompted if they want to lose their changes
before the changes are actually saved. So I've got code that adds a
javascript window.confirm on unload to stop them. Problem is, the check
boxes are run at the server which means whenever somebody clicks one, the
page thinks it's unloading to it prompts them which isn't what I want. I
only want them to be prompted when they are going elsewhere.

Anybody have any suggestions on what I can do to fix this?

TIA - Jeffrey.
 
C

Curt_C [MVP]

UJ said:
I've got a page where users can make changes (enable and disable some
checkboxes.) I need to have it so that if they have made changes and go away
from the page, they will get prompted if they want to lose their changes
before the changes are actually saved. So I've got code that adds a
javascript window.confirm on unload to stop them. Problem is, the check
boxes are run at the server which means whenever somebody clicks one, the
page thinks it's unloading to it prompts them which isn't what I want. I
only want them to be prompted when they are going elsewhere.

Anybody have any suggestions on what I can do to fix this?

TIA - Jeffrey.

tie a local variable to the onBlur event of the checkbox
 
U

UJ

I don't understand what you mean to put a variable in the onBlur event. Sure
I can set a local variable and now I know that something is happened. But
when the person clicks the checkbox, and the page is sent to the server, the
onunload event is called which fires, it sees that there has been a change
so it asks the user if they want to lose the changes then reloads itself.

Are you saying though - check for the variable in the onunload and if the
variable is set, don't do anything because that means that the page isn't
really going to unload - and then reset the variable ? I'm not sure how to
do that.

TIA - Jeffrey.
 
G

Guest

one method would be setting a flag at client side when the check boxes are
clicked and validate that flag inside the unload function

This will be a global flag at client side, on page load set it to true

var FireUnloadPrompt = true;

check boxes will call a client side function oncheck / uncheck

chk1.Attributes.Add("onclick" , "ChecUncheck();")

ChecUncheck()
{
FireUnloadPrompt = false;
return(true);
}

in Window.Unload fucntion

if(FireUnloadPrompt)
code to call window.confirm...

Wouldnt this work?
 
C

Curt_C [MVP]

UJ said:
I don't understand what you mean to put a variable in the onBlur event. Sure
I can set a local variable and now I know that something is happened. But
when the person clicks the checkbox, and the page is sent to the server, the
onunload event is called which fires, it sees that there has been a change
so it asks the user if they want to lose the changes then reloads itself.

Are you saying though - check for the variable in the onunload and if the
variable is set, don't do anything because that means that the page isn't
really going to unload - and then reset the variable ? I'm not sure how to
do that.

TIA - Jeffrey.

tie a local variable to the onBlur event of the checkbox

You will have to turn off the auto-postback
 

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,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top