Check form before unload for changes

Discussion in 'ASP .Net' started by Assimalyst, Aug 10, 2005.

  1. Assimalyst

    Assimalyst Guest

    Hi,

    I have a javascript that checks a form for changes, if it has any it
    alerts the user before they navigate away from the page.

    it can be disabled on a submit button click by adding

    onclick="Javascript:bSubmitted=true;"

    to it's tag.

    However i have a checkbox that has AutoPostBack=true and would like to
    disable it for that too, but if i add

    onCheckChanged="Javascript:bSubmitted=true;"

    I get a compilation error when the page loads

    CS1026: ) expected

    Can anyone suggest a fix? The javascript is below, just incase it's
    useful.

    Thanks

    function checkFormStatus(oForm){
    if(isDirty(oForm))
    event.returnValue = "You have entered form Data without submitting this
    form.";
    }

    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;
    }
     
    Assimalyst, Aug 10, 2005
    #1
    1. Advertising

  2. onCheckChanged is a server-side event handler. You need this to add a client
    side handler:

    myCheckBox.Attributes["onclick"] = "Javascript:bSubmitted=true;";

    Eliyahu

    "Assimalyst" <> wrote in message
    news:...
    > Hi,
    >
    > I have a javascript that checks a form for changes, if it has any it
    > alerts the user before they navigate away from the page.
    >
    > it can be disabled on a submit button click by adding
    >
    > onclick="Javascript:bSubmitted=true;"
    >
    > to it's tag.
    >
    > However i have a checkbox that has AutoPostBack=true and would like to
    > disable it for that too, but if i add
    >
    > onCheckChanged="Javascript:bSubmitted=true;"
    >
    > I get a compilation error when the page loads
    >
    > CS1026: ) expected
    >
    > Can anyone suggest a fix? The javascript is below, just incase it's
    > useful.
    >
    > Thanks
    >
    > function checkFormStatus(oForm){
    > if(isDirty(oForm))
    > event.returnValue = "You have entered form Data without submitting this
    > form.";
    > }
    >
    > 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;
    > }
    >
     
    Eliyahu Goldin, Aug 10, 2005
    #2
    1. Advertising

  3. Assimalyst

    Assimalyst Guest

    Thanks for the suggestion, but i'm pretty new to all this.

    Could you explain in a little more detail how to do this? I can't get
    it to work.

    Thanks again.
     
    Assimalyst, Aug 11, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Justin Bartels
    Replies:
    2
    Views:
    998
    Jacob Yang [MSFT]
    Oct 21, 2003
  2. JIK

    Check for form changes

    JIK, Feb 6, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    579
  3. Casper Skovgaard

    How do I check for changes in a user form?

    Casper Skovgaard, May 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    509
    Rick Spiewak
    May 6, 2004
  4. AmjathKhan via DotNetMonster.com

    Active Element ( Which had the focus ) during form unload in firefox.

    AmjathKhan via DotNetMonster.com, Dec 30, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    540
    AmjathKhan via DotNetMonster.com
    Dec 30, 2005
  5. Al Fraser

    Browser Close / New URL / Form UnLoad()

    Al Fraser, Sep 30, 2003, in forum: Javascript
    Replies:
    1
    Views:
    201
    David Dorward
    Sep 30, 2003
Loading...

Share This Page