prompt to save on exit - disable prompt on save button

M

Mel

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;" />
 
G

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;" />

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
 
S

S. Justin Gengo

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
 
B

bruce barker

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)
 
G

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)

After clicking on the button, the page will be posted back with the
same var needToConfirm = true;
 
M

Mel

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).
 
M

Mel

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)







- 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?
 
G

Guest

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";
}
}
 
M

Mel

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?
 
G

Guest

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) ....
 

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

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top