IsFormDirty

S

Stanley

Hello all,
I have a need to make sure that no users leave a form without being warned
that they have un-saved data. I have the script below that I found on
egghead.com but there is an issue with it that if I have a dropdown that
posts back so that I can show another field if the user chooses other then
the warning pops up. If I use an attribute on the dropdown to use the
OnChange event and set the bSubmitted=True that takes care of the post back
issue but causes another issue. The other issue is now the user can leave
the form without any warning. So what I am looking for is a better way to be
able to handle form post backs and still give the user the warning. Has
anyone done this before?

TIA

-Stanley

[script]
var bSubmitted=false;

function isDirty(oForm)

{

if(bSubmitted)

{

return false;

}

var iNumElems = oForm.elements.length;

for (var i=0;i<iNumElems;i++)

{

var oElem = oForm.elements;

if ("text" == oElem.type || "TEXTAREA" == oElem.tagName)

{

if (oElem.value != oElem.defaultValue)

{

return true;

}

}

else if ("checkbox" == oElem.type || "radio" == oElem.type)

{

if (oElem.checked != oElem.defaultChecked)

{

return true;

}

}

else if ("SELECT" == oElem.tagName)

{

var oOptions = oElem.options;

var iNumOpts = oOptions.length;

for (var j=0;j<iNumOpts;j++)

{

var oOpt = oOptions[j];

if (oOpt.selected != oOpt.defaultSelected)

{

return true;

}

}

}

}

return false;

}

function checkFormStatus(){

var frm = document.forms[0];

if(isDirty(frm))

event.returnValue = "You have entered form Data, or Data has been entered
for you, without submitting this form.";

}

if ( typeof( window.addEventListener ) != "undefined" ) {

window.addEventListener("onbeforeunload", checkFormStatus, false);

} else if ( typeof( window.attachEvent ) != "undefined" ) {

window.attachEvent("onbeforeunload", checkFormStatus);

}

[/script]
 
S

Stanley

No I have not resolved this issue yet.

-Stanley

Alvin Bruney said:
Your post went unanswered. Have you resolved this issue?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Stanley said:
Hello all,
I have a need to make sure that no users leave a form without being warned
that they have un-saved data. I have the script below that I found on
egghead.com but there is an issue with it that if I have a dropdown that
posts back so that I can show another field if the user chooses other then
the warning pops up. If I use an attribute on the dropdown to use the
OnChange event and set the bSubmitted=True that takes care of the post back
issue but causes another issue. The other issue is now the user can leave
the form without any warning. So what I am looking for is a better way
to
be
able to handle form post backs and still give the user the warning. Has
anyone done this before?

TIA

-Stanley

[script]
var bSubmitted=false;

function isDirty(oForm)

{

if(bSubmitted)

{

return false;

}

var iNumElems = oForm.elements.length;

for (var i=0;i<iNumElems;i++)

{

var oElem = oForm.elements;

if ("text" == oElem.type || "TEXTAREA" == oElem.tagName)

{

if (oElem.value != oElem.defaultValue)

{

return true;

}

}

else if ("checkbox" == oElem.type || "radio" == oElem.type)

{

if (oElem.checked != oElem.defaultChecked)

{

return true;

}

}

else if ("SELECT" == oElem.tagName)

{

var oOptions = oElem.options;

var iNumOpts = oOptions.length;

for (var j=0;j<iNumOpts;j++)

{

var oOpt = oOptions[j];

if (oOpt.selected != oOpt.defaultSelected)

{

return true;

}

}

}

}

return false;

}

function checkFormStatus(){

var frm = document.forms[0];

if(isDirty(frm))

event.returnValue = "You have entered form Data, or Data has been entered
for you, without submitting this form.";

}

if ( typeof( window.addEventListener ) != "undefined" ) {

window.addEventListener("onbeforeunload", checkFormStatus, false);

} else if ( typeof( window.attachEvent ) != "undefined" ) {

window.attachEvent("onbeforeunload", checkFormStatus);

}

[/script]

 
A

Alvin Bruney [MVP]

What i think you need, if i understand you correctly, is a validation
control in there. The role of a validation control is to make sure that the
user has entered input before they move on. Have a look at the visual studio
toolbox for the appropriate validation control

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Stanley said:
No I have not resolved this issue yet.

-Stanley

Alvin Bruney said:
Your post went unanswered. Have you resolved this issue?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Stanley said:
Hello all,
I have a need to make sure that no users leave a form without being warned
that they have un-saved data. I have the script below that I found on
egghead.com but there is an issue with it that if I have a dropdown that
posts back so that I can show another field if the user chooses other then
the warning pops up. If I use an attribute on the dropdown to use the
OnChange event and set the bSubmitted=True that takes care of the post back
issue but causes another issue. The other issue is now the user can leave
the form without any warning. So what I am looking for is a better way
to
be
able to handle form post backs and still give the user the warning. Has
anyone done this before?

TIA

-Stanley

[script]
var bSubmitted=false;

function isDirty(oForm)

{

if(bSubmitted)

{

return false;

}

var iNumElems = oForm.elements.length;

for (var i=0;i<iNumElems;i++)

{

var oElem = oForm.elements;

if ("text" == oElem.type || "TEXTAREA" == oElem.tagName)

{

if (oElem.value != oElem.defaultValue)

{

return true;

}

}

else if ("checkbox" == oElem.type || "radio" == oElem.type)

{

if (oElem.checked != oElem.defaultChecked)

{

return true;

}

}

else if ("SELECT" == oElem.tagName)

{

var oOptions = oElem.options;

var iNumOpts = oOptions.length;

for (var j=0;j<iNumOpts;j++)

{

var oOpt = oOptions[j];

if (oOpt.selected != oOpt.defaultSelected)

{

return true;

}

}

}

}

return false;

}

function checkFormStatus(){

var frm = document.forms[0];

if(isDirty(frm))

event.returnValue = "You have entered form Data, or Data has been entered
for you, without submitting this form.";

}

if ( typeof( window.addEventListener ) != "undefined" ) {

window.addEventListener("onbeforeunload", checkFormStatus, false);

} else if ( typeof( window.attachEvent ) != "undefined" ) {

window.attachEvent("onbeforeunload", checkFormStatus);

}

[/script]


 
S

Stanley

I tried that solution and it has the same problem as my code. If I have
autopostback on a dropdown then the user can go away from the page.

-Stanley

Eric said:
See http://www.codeproject.com/aspnet/EWSWebPt2.asp for one possible
solution.


Stanley said:
Hello all,
I have a need to make sure that no users leave a form without being warned
that they have un-saved data. I have the script below that I found on
egghead.com but there is an issue with it that if I have a dropdown that
posts back so that I can show another field if the user chooses other then
the warning pops up. If I use an attribute on the dropdown to use the
OnChange event and set the bSubmitted=True that takes care of the post back
issue but causes another issue. The other issue is now the user can leave
the form without any warning. So what I am looking for is a better way
to
be
able to handle form post backs and still give the user the warning. Has
anyone done this before?

TIA

-Stanley

[script]
var bSubmitted=false;

function isDirty(oForm)

{

if(bSubmitted)

{

return false;

}

var iNumElems = oForm.elements.length;

for (var i=0;i<iNumElems;i++)

{

var oElem = oForm.elements;

if ("text" == oElem.type || "TEXTAREA" == oElem.tagName)

{

if (oElem.value != oElem.defaultValue)

{

return true;

}

}

else if ("checkbox" == oElem.type || "radio" == oElem.type)

{

if (oElem.checked != oElem.defaultChecked)

{

return true;

}

}

else if ("SELECT" == oElem.tagName)

{

var oOptions = oElem.options;

var iNumOpts = oOptions.length;

for (var j=0;j<iNumOpts;j++)

{

var oOpt = oOptions[j];

if (oOpt.selected != oOpt.defaultSelected)

{

return true;

}

}

}

}

return false;

}

function checkFormStatus(){

var frm = document.forms[0];

if(isDirty(frm))

event.returnValue = "You have entered form Data, or Data has been entered
for you, without submitting this form.";

}

if ( typeof( window.addEventListener ) != "undefined" ) {

window.addEventListener("onbeforeunload", checkFormStatus, false);

} else if ( typeof( window.attachEvent ) != "undefined" ) {

window.attachEvent("onbeforeunload", checkFormStatus);

}

[/script]

 
S

Stanley

Actually no Alvin a Validator wont work for what I need. If the user clicks
a link to go to another page that is listed on my menu a validator won't
stop them from going. That was the request made by my boss.

-Stanley

Alvin Bruney said:
What i think you need, if i understand you correctly, is a validation
control in there. The role of a validation control is to make sure that the
user has entered input before they move on. Have a look at the visual studio
toolbox for the appropriate validation control

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Stanley said:
No I have not resolved this issue yet.

-Stanley

Alvin Bruney said:
Your post went unanswered. Have you resolved this issue?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Hello all,
I have a need to make sure that no users leave a form without being warned
that they have un-saved data. I have the script below that I found on
egghead.com but there is an issue with it that if I have a dropdown that
posts back so that I can show another field if the user chooses
other
then
the warning pops up. If I use an attribute on the dropdown to use the
OnChange event and set the bSubmitted=True that takes care of the post
back
issue but causes another issue. The other issue is now the user can leave
the form without any warning. So what I am looking for is a better
way
to
be
able to handle form post backs and still give the user the warning. Has
anyone done this before?

TIA

-Stanley

[script]
var bSubmitted=false;

function isDirty(oForm)

{

if(bSubmitted)

{

return false;

}

var iNumElems = oForm.elements.length;

for (var i=0;i<iNumElems;i++)

{

var oElem = oForm.elements;

if ("text" == oElem.type || "TEXTAREA" == oElem.tagName)

{

if (oElem.value != oElem.defaultValue)

{

return true;

}

}

else if ("checkbox" == oElem.type || "radio" == oElem.type)

{

if (oElem.checked != oElem.defaultChecked)

{

return true;

}

}

else if ("SELECT" == oElem.tagName)

{

var oOptions = oElem.options;

var iNumOpts = oOptions.length;

for (var j=0;j<iNumOpts;j++)

{

var oOpt = oOptions[j];

if (oOpt.selected != oOpt.defaultSelected)

{

return true;

}

}

}

}

return false;

}

function checkFormStatus(){

var frm = document.forms[0];

if(isDirty(frm))

event.returnValue = "You have entered form Data, or Data has been entered
for you, without submitting this form.";

}

if ( typeof( window.addEventListener ) != "undefined" ) {

window.addEventListener("onbeforeunload", checkFormStatus, false);

} else if ( typeof( window.attachEvent ) != "undefined" ) {

window.attachEvent("onbeforeunload", checkFormStatus);

}

[/script]


 
A

Alvin Bruney [MVP]

Every postback causes page_load to fire. Put code in your page load to
determine whether or not the action should be allowed. If it's not a
postback, you can easily handle it thru scripting or validation.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Stanley said:
Actually no Alvin a Validator wont work for what I need. If the user clicks
a link to go to another page that is listed on my menu a validator won't
stop them from going. That was the request made by my boss.

-Stanley

Alvin Bruney said:
What i think you need, if i understand you correctly, is a validation
control in there. The role of a validation control is to make sure that the
user has entered input before they move on. Have a look at the visual studio
toolbox for the appropriate validation control

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Stanley said:
No I have not resolved this issue yet.

-Stanley

"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
Your post went unanswered. Have you resolved this issue?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Hello all,
I have a need to make sure that no users leave a form without being
warned
that they have un-saved data. I have the script below that I found on
egghead.com but there is an issue with it that if I have a
dropdown
that
posts back so that I can show another field if the user chooses other
then
the warning pops up. If I use an attribute on the dropdown to use the
OnChange event and set the bSubmitted=True that takes care of the post
back
issue but causes another issue. The other issue is now the user can
leave
the form without any warning. So what I am looking for is a better way
to
be
able to handle form post backs and still give the user the
warning.
Has
anyone done this before?

TIA

-Stanley

[script]
var bSubmitted=false;

function isDirty(oForm)

{

if(bSubmitted)

{

return false;

}

var iNumElems = oForm.elements.length;

for (var i=0;i<iNumElems;i++)

{

var oElem = oForm.elements;

if ("text" == oElem.type || "TEXTAREA" == oElem.tagName)

{

if (oElem.value != oElem.defaultValue)

{

return true;

}

}

else if ("checkbox" == oElem.type || "radio" == oElem.type)

{

if (oElem.checked != oElem.defaultChecked)

{

return true;

}

}

else if ("SELECT" == oElem.tagName)

{

var oOptions = oElem.options;

var iNumOpts = oOptions.length;

for (var j=0;j<iNumOpts;j++)

{

var oOpt = oOptions[j];

if (oOpt.selected != oOpt.defaultSelected)

{

return true;

}

}

}

}

return false;

}

function checkFormStatus(){

var frm = document.forms[0];

if(isDirty(frm))

event.returnValue = "You have entered form Data, or Data has been
entered
for you, without submitting this form.";

}

if ( typeof( window.addEventListener ) != "undefined" ) {

window.addEventListener("onbeforeunload", checkFormStatus, false);

} else if ( typeof( window.attachEvent ) != "undefined" ) {

window.attachEvent("onbeforeunload", checkFormStatus);

}

[/script]


 
E

Eric

There are properties in the page class that can control this. On postback,
it will pass the dirty state back to the server and maintains it across
postbacks. For controls that use auto-postback, the page's Dirty property
can be set to true in the event handler explicitly so that after returning
to the client, attempts to leave the page will always cause prompting.
There are also properties that allow filtering controls out of the prompting
behavior (such as postback fields and Save buttons) so that you don't get
prompted when they postback but it still maintains the dirty state. The
demo app for the article shows this in action. It uses buttons for the
postback but the concept is the same. If you go to the General.aspx page
and click on the Defaults button, it posts back, stores some values to the
text boxes, sets the Dirty flag to true and returns to the client. If you
now attempt to navigate away from the page without clicking Save, it prompts
you first. If you enter data in the fields first before clicking the
Defaults button, you also get the warning. This can be changed by adding
the button's ID to the BypassPromptIDs list like it does for the two
Enable/Disable buttons and the Save button.

Eric

Stanley said:
I tried that solution and it has the same problem as my code. If I have
autopostback on a dropdown then the user can go away from the page.

-Stanley

Eric said:
See http://www.codeproject.com/aspnet/EWSWebPt2.asp for one possible
solution.


Stanley said:
Hello all,
I have a need to make sure that no users leave a form without being warned
that they have un-saved data. I have the script below that I found on
egghead.com but there is an issue with it that if I have a dropdown that
posts back so that I can show another field if the user chooses other then
the warning pops up. If I use an attribute on the dropdown to use the
OnChange event and set the bSubmitted=True that takes care of the post back
issue but causes another issue. The other issue is now the user can leave
the form without any warning. So what I am looking for is a better way
to
be
able to handle form post backs and still give the user the warning. Has
anyone done this before?

TIA

-Stanley

[script]
var bSubmitted=false;

function isDirty(oForm)

{

if(bSubmitted)

{

return false;

}

var iNumElems = oForm.elements.length;

for (var i=0;i<iNumElems;i++)

{

var oElem = oForm.elements;

if ("text" == oElem.type || "TEXTAREA" == oElem.tagName)

{

if (oElem.value != oElem.defaultValue)

{

return true;

}

}

else if ("checkbox" == oElem.type || "radio" == oElem.type)

{

if (oElem.checked != oElem.defaultChecked)

{

return true;

}

}

else if ("SELECT" == oElem.tagName)

{

var oOptions = oElem.options;

var iNumOpts = oOptions.length;

for (var j=0;j<iNumOpts;j++)

{

var oOpt = oOptions[j];

if (oOpt.selected != oOpt.defaultSelected)

{

return true;

}

}

}

}

return false;

}

function checkFormStatus(){

var frm = document.forms[0];

if(isDirty(frm))

event.returnValue = "You have entered form Data, or Data has been entered
for you, without submitting this form.";

}

if ( typeof( window.addEventListener ) != "undefined" ) {

window.addEventListener("onbeforeunload", checkFormStatus, false);

} else if ( typeof( window.attachEvent ) != "undefined" ) {

window.attachEvent("onbeforeunload", checkFormStatus);

}

[/script]


 

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,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top