prompt to save on exit - disable prompt on save button

Discussion in 'ASP .Net' started by Mel, Dec 17, 2008.

  1. Mel

    Mel Guest

    I am using the code below to prompt to save on exit of my asp.net page
    but don't prompt when the Save button is clicked. However, I am using
    a WebControls button for the Save button, so when the user clicks the
    save button it runs the cmdSave_Click event server-side vb code.
    Since I am using a web control, how do I fix this code to set the
    needToConfirm variable correctly?

    code below is from: http://www.4guysfromrolla.com/webtech/100604-1.shtml
    <script language="JavaScript">
    var needToConfirm = true;

    window.onbeforeunload = confirmExit;
    function confirmExit()
    {
    if (needToConfirm)
    return message to display in dialog box;
    }
    </script>

    ....

    <input type="Submit" value="Save" onclick="needToConfirm = false;" />
    Mel, Dec 17, 2008
    #1
    1. Advertising

  2. On Dec 17, 8:52 pm, Mel <> wrote:
    > I am using the code below to prompt to save on exit of my asp.net page
    > but don't prompt when the Save button is clicked.  However, I am using
    > a WebControls button for the Save button, so when the user clicks the
    > save button it runs the cmdSave_Click event server-side vb code.
    > Since I am using a web control, how do I fix this code to set the
    > needToConfirm variable correctly?
    >
    > code below is from:http://www.4guysfromrolla.com/webtech/100604-1.shtml
    > <script language="JavaScript">
    >   var needToConfirm = true;
    >
    >   window.onbeforeunload = confirmExit;
    >   function confirmExit()
    >   {
    >     if (needToConfirm)
    >       return message to display in dialog box;
    >   }
    > </script>
    >
    > ...
    >
    > <input type="Submit" value="Save" onclick="needToConfirm = false;" />


    You can inject a value to javascript from code-behind.

    Change var needToConfirm = true; to

    var needToConfirm = <%=neeToConfirm%>;

    add a protected variable in the class

    protected string needToConfirm = "";

    by default set it to "true" and change to "false" once button is
    clicked

    Hope this helps
    Alexey Smirnov, Dec 17, 2008
    #2
    1. Advertising

  3. Mel,

    I created a free - including source code - javascript assembly that has this
    built into it. I use the same technique as the article you refer to but have
    made it very easy to hook up to any button click / controls to trigger the
    warning.

    I can't be positive that it will solve your problem but at the least the
    source code should help you make the changes you're referring to. The demo
    can be viewed here:
    http://www.aboutfortunate.com/Component-Library/JavaScript-Object.aspx

    And the source code downloaded here:
    http://www.aboutfortunate.com/Component-Library.aspx

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer

    Free code library at:
    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche




    "Mel" <> wrote in message
    news:...
    >I am using the code below to prompt to save on exit of my asp.net page
    > but don't prompt when the Save button is clicked. However, I am using
    > a WebControls button for the Save button, so when the user clicks the
    > save button it runs the cmdSave_Click event server-side vb code.
    > Since I am using a web control, how do I fix this code to set the
    > needToConfirm variable correctly?
    >
    > code below is from: http://www.4guysfromrolla.com/webtech/100604-1.shtml
    > <script language="JavaScript">
    > var needToConfirm = true;
    >
    > window.onbeforeunload = confirmExit;
    > function confirmExit()
    > {
    > if (needToConfirm)
    > return message to display in dialog box;
    > }
    > </script>
    >
    > ...
    >
    > <input type="Submit" value="Save" onclick="needToConfirm = false;" />
    S. Justin Gengo, Dec 17, 2008
    #3
  4. Mel

    bruce barker Guest

    use the OnClientClick property to do the same. if you use validation, you
    will need to call the Page_ClientValidate

    <asp:button
    id="btnSave"
    runat="server"
    onclientclick="if (Page_ClientValidate()) needToConfirm = false; "
    />

    -- bruce (sqlwork.com)


    "Mel" wrote:

    > I am using the code below to prompt to save on exit of my asp.net page
    > but don't prompt when the Save button is clicked. However, I am using
    > a WebControls button for the Save button, so when the user clicks the
    > save button it runs the cmdSave_Click event server-side vb code.
    > Since I am using a web control, how do I fix this code to set the
    > needToConfirm variable correctly?
    >
    > code below is from: http://www.4guysfromrolla.com/webtech/100604-1.shtml
    > <script language="JavaScript">
    > var needToConfirm = true;
    >
    > window.onbeforeunload = confirmExit;
    > function confirmExit()
    > {
    > if (needToConfirm)
    > return message to display in dialog box;
    > }
    > </script>
    >
    > ....
    >
    > <input type="Submit" value="Save" onclick="needToConfirm = false;" />
    >
    bruce barker, Dec 17, 2008
    #4
  5. On Dec 17, 9:55 pm, bruce barker
    <> wrote:
    > use the OnClientClick property to do the same. if you use validation, you
    > will need to call the Page_ClientValidate
    >
    > <asp:button
    >     id="btnSave"
    >     runat="server"
    >     onclientclick="if (Page_ClientValidate()) needToConfirm = false; "
    > />
    >
    > -- bruce (sqlwork.com)
    >
    > "Mel" wrote:
    > > I am using the code below to prompt to save on exit of my asp.net page
    > > but don't prompt when the Save button is clicked.  However, I am using
    > > a WebControls button for the Save button, so when the user clicks the
    > > save button it runs the cmdSave_Click event server-side vb code.
    > > Since I am using a web control, how do I fix this code to set the
    > > needToConfirm variable correctly?

    >
    > > code below is from:http://www.4guysfromrolla.com/webtech/100604-1.shtml
    > > <script language="JavaScript">
    > >   var needToConfirm = true;

    >
    > >   window.onbeforeunload = confirmExit;
    > >   function confirmExit()
    > >   {
    > >     if (needToConfirm)
    > >       return message to display in dialog box;
    > >   }
    > > </script>

    >
    > > ....

    >
    > > <input type="Submit" value="Save" onclick="needToConfirm = false;" />


    After clicking on the button, the page will be posted back with the
    same var needToConfirm = true;
    Alexey Smirnov, Dec 17, 2008
    #5
  6. Mel

    Mel Guest

    On Dec 17, 2:23 pm, Alexey Smirnov <> wrote:
    > On Dec 17, 8:52 pm, Mel <> wrote:
    >
    >
    >
    >
    >
    > > I am using the code below to prompt to save on exit of my asp.net page
    > > but don't prompt when the Save button is clicked.  However, I am using
    > > a WebControls button for the Save button, so when the user clicks the
    > > save button it runs the cmdSave_Click event server-side vb code.
    > > Since I am using a web control, how do I fix this code to set the
    > > needToConfirm variable correctly?

    >
    > > code below is from:http://www.4guysfromrolla.com/webtech/100604-1.shtml
    > > <script language="JavaScript">
    > >   var needToConfirm = true;

    >
    > >   window.onbeforeunload = confirmExit;
    > >   function confirmExit()
    > >   {
    > >     if (needToConfirm)
    > >       return message to display in dialog box;
    > >   }
    > > </script>

    >
    > > ...

    >
    > > <input type="Submit" value="Save" onclick="needToConfirm = false;" />

    >
    > You can inject a value to javascript from code-behind.
    >
    > Change var needToConfirm = true; to
    >
    > var needToConfirm = <%=neeToConfirm%>;
    >
    > add a protected variable in the class
    >
    > protected string needToConfirm = "";
    >
    > by default set it to "true" and change to "false" once button is
    > clicked
    >
    > Hope this helps- Hide quoted text -
    >
    > - Show quoted text -


    Hi Alexey,
    Thank you for responding. I tried what you said. But when I click
    the save button it still prompts with the "You have attempted to leave
    this page..." message. Probably because the Javascript runs (which
    sets the variable to true) before the server-side code runs. I am
    setting the needToConfirm=false but it exists on the server side code
    (cmdSave_Click).
    Mel, Dec 18, 2008
    #6
  7. Mel

    Mel Guest

    On Dec 17, 2:55 pm, bruce barker
    <> wrote:
    > use the OnClientClick property to do the same. if you use validation, you
    > will need to call the Page_ClientValidate
    >
    > <asp:button
    >     id="btnSave"
    >     runat="server"
    >     onclientclick="if (Page_ClientValidate()) needToConfirm = false; "
    > />
    >
    > -- bruce (sqlwork.com)
    >
    >
    >
    > "Mel" wrote:
    > > I am using the code below to prompt to save on exit of my asp.net page
    > > but don't prompt when the Save button is clicked.  However, I am using
    > > a WebControls button for the Save button, so when the user clicks the
    > > save button it runs the cmdSave_Click event server-side vb code.
    > > Since I am using a web control, how do I fix this code to set the
    > > needToConfirm variable correctly?

    >
    > > code below is from:http://www.4guysfromrolla.com/webtech/100604-1.shtml
    > > <script language="JavaScript">
    > >   var needToConfirm = true;

    >
    > >   window.onbeforeunload = confirmExit;
    > >   function confirmExit()
    > >   {
    > >     if (needToConfirm)
    > >       return message to display in dialog box;
    > >   }
    > > </script>

    >
    > > ....

    >
    > > <input type="Submit" value="Save" onclick="needToConfirm = false;" />- Hide quoted text -

    >
    > - Show quoted text -


    Okay cool, I've got the save button fixed. I have a "Find" pop-up
    window that asks for a fab number and when they click OK the message
    appears asking them if they want to save before navigating to the new
    record. If the user presses cancel my javascript code still writes
    the fab number data to the screen. Any way to tell if the user
    clicked on cancel?
    Mel, Dec 18, 2008
    #7
  8. On Dec 18, 9:09 pm, Mel <> wrote:
    > Hi Alexey,
    > Thank you for responding.  I tried what you said.  But when I click
    > the save button it still prompts with the "You have attempted to leave
    > this page..." message.  Probably because the Javascript runs (which
    > sets the variable to true) before the server-side code runs.  I am
    > setting the needToConfirm=false but it exists on the server side code
    > (cmdSave_Click).


    Well, this is because when you click on the button you go to re-load
    page in the browser. To avoid this message you still need change the
    value of neeToConfirm variable like it was in the original example:
    onclick="needToConfirm = false;". But because you wanted to have a
    server control you should use onclientclick="needToConfirm = false;".
    So this

    <input type="Submit" value="Save" onclick="needToConfirm = false;" />

    becomes

    <asp:Button ID="Submit" runat="server" Text="Save"
    OnClick="Submit_Click" onclientclick="needToConfirm = false;" />

    After we saved the data, we need to reset needToConfirm from the code-
    behind, so do following

    protected void Submit_Click(object sender, EventArgs e)
    {
    ..... here is some code to save data .....

    confirmExit = "false";
    }

    and we're done...

    Here's complete example:

    <%@ Page Language="C#" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

    <html>
    <head>

    <script language="JavaScript">
    var needToConfirm = <%=confirmExit%>;

    window.onbeforeunload = confirmExit;
    function confirmExit() {
    if (needToConfirm)
    return "Are you sure?";
    }
    </script>

    </head>
    <body>
    <form id="form1" name="form1" runat="server">
    <asp:Button ID="Submit" runat="server" Text="Save"
    OnClick="Submit_Click" onclientclick="needToConfirm = false;" />
    </form>
    </body>
    </html>

    in code-behind

    public partial class _Default : System.Web.UI.Page
    {
    protected string confirmExit = "true";

    protected void Submit_Click(object sender, EventArgs e)
    {
    confirmExit = "false";
    }
    }
    Alexey Smirnov, Dec 19, 2008
    #8
  9. Mel

    Mel Guest

    On Dec 19, 2:38 am, Alexey Smirnov <> wrote:
    > On Dec 18, 9:09 pm, Mel <> wrote:
    >
    > > Hi Alexey,
    > > Thank you for responding.  I tried what you said.  But when I click
    > > the save button it still prompts with the "You have attempted to leave
    > > this page..." message.  Probably because the Javascript runs (which
    > > sets the variable to true) before the server-side code runs.  I am
    > > setting the needToConfirm=false but it exists on the server side code
    > > (cmdSave_Click).

    >
    > Well, this is because when you click on the button you go to re-load
    > page in the browser. To avoid this message you still need change the
    > value of neeToConfirm variable like it was in the original example:
    > onclick="needToConfirm = false;". But because you wanted to have a
    > server control you should use onclientclick="needToConfirm = false;".
    > So this
    >
    > <input type="Submit" value="Save" onclick="needToConfirm = false;" />
    >
    > becomes
    >
    > <asp:Button ID="Submit" runat="server" Text="Save"
    > OnClick="Submit_Click" onclientclick="needToConfirm = false;" />
    >
    > After we saved the data, we need to reset needToConfirm from the code-
    > behind, so do following
    >
    > protected void Submit_Click(object sender, EventArgs e)
    > {
    > .... here is some code to save data .....
    >
    > confirmExit = "false";
    >
    > }
    >
    > and we're done...
    >
    > Here's complete example:
    >
    > <%@ Page Language="C#" AutoEventWireup="true"
    > CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
    >
    > <html>
    > <head>
    >
    >     <script language="JavaScript">
    >         var needToConfirm = <%=confirmExit%>;
    >
    >         window.onbeforeunload = confirmExit;
    >         function confirmExit() {
    >             if (needToConfirm)
    >                 return "Are you sure?";
    >         }
    >     </script>
    >
    > </head>
    > <body>
    >     <form id="form1" name="form1" runat="server">
    >     <asp:Button ID="Submit" runat="server" Text="Save"
    > OnClick="Submit_Click" onclientclick="needToConfirm = false;" />
    >     </form>
    > </body>
    > </html>
    >
    > in code-behind
    >
    > public partial class _Default : System.Web.UI.Page
    >     {
    >         protected string confirmExit = "true";
    >
    >         protected void Submit_Click(object sender, EventArgs e)
    >         {
    >             confirmExit = "false";
    >         }
    >     }


    Okay cool, I've got the save button fixed. I have a "Find" pop-up
    window that asks for a fab number and when they click OK the message
    appears asking them if they want to save before navigating to the new
    record. If the user presses cancel my javascript code still writes
    the fab number data to the screen. Any way to tell if the user
    clicked on cancel?
    Mel, Dec 19, 2008
    #9
  10. On Dec 19, 3:51 pm, Mel <> wrote:
    > On Dec 19, 2:38 am, Alexey Smirnov <> wrote:
    >
    >
    >
    > > On Dec 18, 9:09 pm, Mel <> wrote:

    >
    > > > Hi Alexey,
    > > > Thank you for responding.  I tried what you said.  But when I click
    > > > the save button it still prompts with the "You have attempted to leave
    > > > this page..." message.  Probably because the Javascript runs (which
    > > > sets the variable to true) before the server-side code runs.  I am
    > > > setting the needToConfirm=false but it exists on the server side code
    > > > (cmdSave_Click).

    >
    > > Well, this is because when you click on the button you go to re-load
    > > page in the browser. To avoid this message you still need change the
    > > value of neeToConfirm variable like it was in the original example:
    > > onclick="needToConfirm = false;". But because you wanted to have a
    > > server control you should use onclientclick="needToConfirm = false;".
    > > So this

    >
    > > <input type="Submit" value="Save" onclick="needToConfirm = false;" />

    >
    > > becomes

    >
    > > <asp:Button ID="Submit" runat="server" Text="Save"
    > > OnClick="Submit_Click" onclientclick="needToConfirm = false;" />

    >
    > > After we saved the data, we need to reset needToConfirm from the code-
    > > behind, so do following

    >
    > > protected void Submit_Click(object sender, EventArgs e)
    > > {
    > > .... here is some code to save data .....

    >
    > > confirmExit = "false";

    >
    > > }

    >
    > > and we're done...

    >
    > > Here's complete example:

    >
    > > <%@ Page Language="C#" AutoEventWireup="true"
    > > CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

    >
    > > <html>
    > > <head>

    >
    > >     <script language="JavaScript">
    > >         var needToConfirm = <%=confirmExit%>;

    >
    > >         window.onbeforeunload = confirmExit;
    > >         function confirmExit() {
    > >             if (needToConfirm)
    > >                 return "Are you sure?";
    > >         }
    > >     </script>

    >
    > > </head>
    > > <body>
    > >     <form id="form1" name="form1" runat="server">
    > >     <asp:Button ID="Submit" runat="server" Text="Save"
    > > OnClick="Submit_Click" onclientclick="needToConfirm = false;" />
    > >     </form>
    > > </body>
    > > </html>

    >
    > > in code-behind

    >
    > > public partial class _Default : System.Web.UI.Page
    > >     {
    > >         protected string confirmExit = "true";

    >
    > >         protected void Submit_Click(object sender, EventArgs e)
    > >         {
    > >             confirmExit = "false";
    > >         }
    > >     }

    >
    > Okay cool, I've got the save button fixed.  I have a "Find" pop-up
    > window that asks for a fab number and when they click OK the message
    > appears asking them if they want to save before navigating to the new
    > record.  If the user presses cancel my javascript code still writes
    > the fab number data to the screen.  Any way to tell if the user
    > clicked on cancel?


    I think you need to check if (needToConfirm) ....
    Alexey Smirnov, Dec 19, 2008
    #10
  11. Mel

    Sailaja Appi Guest

    Sailaja Appi, Feb 13, 2009
    #11
    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. =?Utf-8?B?U2FuZHk=?=

    Code to Exit Web App and Exit Internet Explorer

    =?Utf-8?B?U2FuZHk=?=, Aug 3, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    7,880
    =?Utf-8?B?U2FuZHk=?=
    Aug 5, 2005
  2. Joe Smith
    Replies:
    4
    Views:
    65,795
    sandeep1976
    Nov 8, 2006
  3. Replies:
    2
    Views:
    444
    Jeff Epler
    May 31, 2005
  4. QQ
    Replies:
    5
    Views:
    507
    Jonathan Adams
    May 10, 2005
  5. SteveSu
    Replies:
    3
    Views:
    522
    Dominick Baier [DevelopMentor]
    Aug 25, 2005
Loading...

Share This Page