Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > prompt to save on exit - disable prompt on save button

Reply
Thread Tools

prompt to save on exit - disable prompt on save button

 
 
Mel
Guest
Posts: n/a
 
      12-17-2008
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;" />
 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      12-17-2008
On Dec 17, 8:52*pm, Mel <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
S. Justin Gengo
Guest
Posts: n/a
 
      12-17-2008
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/Compon...pt-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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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;" />


 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      12-17-2008
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;" />
>

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      12-17-2008
On Dec 17, 9:55*pm, bruce barker
<(E-Mail Removed)> 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;
 
Reply With Quote
 
Mel
Guest
Posts: n/a
 
      12-18-2008
On Dec 17, 2:23*pm, Alexey Smirnov <(E-Mail Removed)> wrote:
> On Dec 17, 8:52*pm, Mel <(E-Mail Removed)> 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).
 
Reply With Quote
 
Mel
Guest
Posts: n/a
 
      12-18-2008
On Dec 17, 2:55*pm, bruce barker
<(E-Mail Removed)> 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?
 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      12-19-2008
On Dec 18, 9:09*pm, Mel <(E-Mail Removed)> 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";
}
}
 
Reply With Quote
 
Mel
Guest
Posts: n/a
 
      12-19-2008
On Dec 19, 2:38*am, Alexey Smirnov <(E-Mail Removed)> wrote:
> On Dec 18, 9:09*pm, Mel <(E-Mail Removed)> 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?
 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      12-19-2008
On Dec 19, 3:51*pm, Mel <(E-Mail Removed)> wrote:
> On Dec 19, 2:38*am, Alexey Smirnov <(E-Mail Removed)> wrote:
>
>
>
> > On Dec 18, 9:09*pm, Mel <(E-Mail Removed)> 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) ....
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exit code of a batch (using exit /B) Joe Smith Java 4 11-08-2006 12:25 PM
Disable "prompt me to save passwords" programaticly? SteveSu ASP .Net Security 3 08-25-2005 09:51 PM
Code to Exit Web App and Exit Internet Explorer =?Utf-8?B?U2FuZHk=?= ASP .Net 7 08-05-2005 01:55 AM
exit after process exit ajikoe@gmail.com Python 2 05-31-2005 08:11 PM
What's the difference of return 0; exit(0);exit(1) QQ C Programming 5 05-10-2005 10:11 PM



Advertisments