473,856 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JavaScript confirm

I have gone through a no. of posts in this NewsGroup regarding my
problem but alas, couldn't come across one which would have helped me
in resolving the issue. My problem is this:

An ASPX Form has a Button. When the Button is clicked, I want a
JavaScript confirm dialog to pop-up with the options 'OK' & 'Cancel'. I
have done this using the following code:

Sub Page_Load(....)
btnClick.Attrib utes.Add("OnCli ck", "javascript:ret urn confirm('Are
you sure you want to exit?');")
End Sub

where 'btnClick' is the name of the Button.

The problem is how do I find out which of the 2 buttons, 'OK' or
'Cancel', has a user clicked? Any one can help me out with this?

My primary intention is if the user clicks 'OK' in the confirm dialog,
then function1 should get executed but if the user clicks 'Cancel',
then function2 should get executed.

In JavaScript, if 'OK' is clicked, then confiorm returns 1; if 'Cancel'
is clicked, then confirm returns 0.

Oct 26 '06 #1
8 4038
Are you asking on the server side? Your code already does it. If Cancel is
clicked, the event won't propagate to the server.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
<rn**@rediffmai l.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
>I have gone through a no. of posts in this NewsGroup regarding my
problem but alas, couldn't come across one which would have helped me
in resolving the issue. My problem is this:

An ASPX Form has a Button. When the Button is clicked, I want a
JavaScript confirm dialog to pop-up with the options 'OK' & 'Cancel'. I
have done this using the following code:

Sub Page_Load(....)
btnClick.Attrib utes.Add("OnCli ck", "javascript:ret urn confirm('Are
you sure you want to exit?');")
End Sub

where 'btnClick' is the name of the Button.

The problem is how do I find out which of the 2 buttons, 'OK' or
'Cancel', has a user clicked? Any one can help me out with this?

My primary intention is if the user clicks 'OK' in the confirm dialog,
then function1 should get executed but if the user clicks 'Cancel',
then function2 should get executed.

In JavaScript, if 'OK' is clicked, then confiorm returns 1; if 'Cancel'
is clicked, then confirm returns 0.

Oct 26 '06 #2
>>If Cancel is
>clicked, the event won't propagate to the server
But I want the event to propogate so that ASP.NET can invoke a
different sub-routine.

As already said, I want the confirm dialog to come up when a Button is
clicked. The code snippet I showed in post #1 does this. Now I want to
invoke a sub-routine when the user clicks 'OK' which can be easily done
using the OnClick event of the Button control using the following code
snippet (assume that this ASPX page is named 'Proceed.aspx')

<script runat="server">
Sub Page_Load(..... )
btnClick.Attrib utes.Add("OnCli ck", "javascript:ret urn
confirm('Are you sure you wish to exit?');")
End Sub

Sub OKClicked(obj As Object, ea As EventArgs)
Response.Write( "You clicked OK")
End Sub
</script>
<form runat="server">
<asp:Button ID="btnClick" OnClick="OKClic ked" Text="PROCEED"
runat="server"/>
</form>

But if the user clicks 'Cancel' in the confirm dialog, I want ANOTHER
sub-routine named, say, 'CancelClicked' to be invoked. How do I invoke
the sub 'CancelClicked' when the user clicks 'Cancel' in the confirm
dialog? This is where I am getting stuck.

Actually prior to coming to this ASPX page named 'Proceed.aspx' that
has the above Button control, a few records entered by a user in
different Form fields get inserted in a temporary SQL Server 2005 DB
table. A user clicks this Button control; the JavaScript confirm dialog
with 'OK' & 'Cancel' buttons pops-up. If the user clicks 'OK', records
in the temporary table will be transferred to another DB table after
which, the records in the temporary table will get deleted. However, if
the user clicks 'Cancel' in the confirm dialog, then WITHOUT
transferring the records from the temporary table to the other DB
table, the records in the temporary table should get deleted.

That's precisely the reason why I want to execute a sub-routine even
when 'Cancel' in the confirm dialog is clicked.
Eliyahu Goldin wrote:
Are you asking on the server side? Your code already does it. If Cancel is
clicked, the event won't propagate to the server.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
<rn**@rediffmai l.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
I have gone through a no. of posts in this NewsGroup regarding my
problem but alas, couldn't come across one which would have helped me
in resolving the issue. My problem is this:

An ASPX Form has a Button. When the Button is clicked, I want a
JavaScript confirm dialog to pop-up with the options 'OK' & 'Cancel'. I
have done this using the following code:

Sub Page_Load(....)
btnClick.Attrib utes.Add("OnCli ck", "javascript:ret urn confirm('Are
you sure you want to exit?');")
End Sub

where 'btnClick' is the name of the Button.

The problem is how do I find out which of the 2 buttons, 'OK' or
'Cancel', has a user clicked? Any one can help me out with this?

My primary intention is if the user clicks 'OK' in the confirm dialog,
then function1 should get executed but if the user clicks 'Cancel',
then function2 should get executed.

In JavaScript, if 'OK' is clicked, then confiorm returns 1; if 'Cancel'
is clicked, then confirm returns 0.
Oct 26 '06 #3
If you don't return "false", the event will arrive to the server. Now you
need to pass the user's choice in a hidden input control:

btnClick.Attrib utes.Add("OnCli ck",
"javascript:myF orm.inhConfirmR esponse.value = confirm('Are you sure you wish
to exit?') ? 'true' : 'false'");

and in the aspx page:

<input type="hidden" id="inhConfirmR esponse" runat="server" />

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
<rn**@rediffmai l.comwrote in message
news:11******** *************@e 3g2000cwe.googl egroups.com...
>>>If Cancel is
clicked, the event won't propagate to the server

But I want the event to propogate so that ASP.NET can invoke a
different sub-routine.

As already said, I want the confirm dialog to come up when a Button is
clicked. The code snippet I showed in post #1 does this. Now I want to
invoke a sub-routine when the user clicks 'OK' which can be easily done
using the OnClick event of the Button control using the following code
snippet (assume that this ASPX page is named 'Proceed.aspx')

<script runat="server">
Sub Page_Load(..... )
btnClick.Attrib utes.Add("OnCli ck", "javascript:ret urn
confirm('Are you sure you wish to exit?');")
End Sub

Sub OKClicked(obj As Object, ea As EventArgs)
Response.Write( "You clicked OK")
End Sub
</script>
<form runat="server">
<asp:Button ID="btnClick" OnClick="OKClic ked" Text="PROCEED"
runat="server"/>
</form>

But if the user clicks 'Cancel' in the confirm dialog, I want ANOTHER
sub-routine named, say, 'CancelClicked' to be invoked. How do I invoke
the sub 'CancelClicked' when the user clicks 'Cancel' in the confirm
dialog? This is where I am getting stuck.

Actually prior to coming to this ASPX page named 'Proceed.aspx' that
has the above Button control, a few records entered by a user in
different Form fields get inserted in a temporary SQL Server 2005 DB
table. A user clicks this Button control; the JavaScript confirm dialog
with 'OK' & 'Cancel' buttons pops-up. If the user clicks 'OK', records
in the temporary table will be transferred to another DB table after
which, the records in the temporary table will get deleted. However, if
the user clicks 'Cancel' in the confirm dialog, then WITHOUT
transferring the records from the temporary table to the other DB
table, the records in the temporary table should get deleted.

That's precisely the reason why I want to execute a sub-routine even
when 'Cancel' in the confirm dialog is clicked.
Eliyahu Goldin wrote:
>Are you asking on the server side? Your code already does it. If Cancel
is
clicked, the event won't propagate to the server.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
<rn**@rediffma il.comwrote in message
news:11******* *************** @b28g2000cwb.go oglegroups.com. ..
>I have gone through a no. of posts in this NewsGroup regarding my
problem but alas, couldn't come across one which would have helped me
in resolving the issue. My problem is this:

An ASPX Form has a Button. When the Button is clicked, I want a
JavaScript confirm dialog to pop-up with the options 'OK' & 'Cancel'. I
have done this using the following code:

Sub Page_Load(....)
btnClick.Attrib utes.Add("OnCli ck", "javascript:ret urn confirm('Are
you sure you want to exit?');")
End Sub

where 'btnClick' is the name of the Button.

The problem is how do I find out which of the 2 buttons, 'OK' or
'Cancel', has a user clicked? Any one can help me out with this?

My primary intention is if the user clicks 'OK' in the confirm dialog,
then function1 should get executed but if the user clicks 'Cancel',
then function2 should get executed.

In JavaScript, if 'OK' is clicked, then confiorm returns 1; if 'Cancel'
is clicked, then confirm returns 0.

Oct 26 '06 #4
That's exactly what I was looking out for. However, I created a custom
control to do the same but this also gives the user the option to
customize the confirm message text dynamically in the confirm dialog.
This is the VB class file (Confirm.vb):

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.W ebControls

Namespace Confirm
Public Class ShowConfirm : Inherits Control
Private strConfirmMsg As String
Public txtGetAnswer As TextBox

Public Property ConfirmMessage( ) As String
Get
ConfirmMessage = strConfirmMsg
End Get
Set(ByVal value As String)
strConfirmMsg = value
End Set
End Property

Protected Overrides Sub Render(ByVal Output As HtmlTextWriter)
Output.Write("< script language='JavaS cript'>")
Output.Write(vb CrLf)
Output.Write("v ar answer = confirm('")
Output.Write(st rConfirmMsg)
Output.Write("' )")
Output.Write(vb CrLf)
Output.Write("d ocument.frmCC.t xtGetAnswer.val ue = answer")
Output.Write(vb CrLf)
Output.Write("</script>")
End Sub
End Class
End Namespace

Using vbc, I compiled the above class into a DLL named 'ConfirmCC.dll' .
The ASPX page which uses this custom control has 2 TextBox controls & a
Button control. In the first TextBox, user enters the question he wants
to ask & depending upon whether the user clicks 'OK' or 'Cancel' in the
confirm dialog (which comes up when the Button is clicked), the second
TextBox displays true or false respectively.

<%@ Register TagPrefix="CC" Namespace="Conf irm" Assembly="Confi rmCC" %>

<script runat="server">
Sub ConfirmSub(ByVa l obj As Object, ByVal ea As EventArgs)
ccConfirm.Confi rmMessage = txtMsg.Text
End Sub
</script>
<form id="frmCC" runat="server">
<asp:TextBox ID="txtMsg" runat="server"/>
<asp:TextBox ID="txtGetAnswe r" runat="server"/>
<asp:Button ID="btnConfirm " OnClick="Confir mSub" Text="CONFIRM"
runat="server"/>
<CC:ShowConfi rm ID="ccConfirm" runat="server"/>
</form>

If the user enters the text 'Do you want to proceed ahead?' in the
first TextBox & clicks the Button, the confirm dialog asks the question
'Do you want to proceed ahead?' or if the user enters the text 'Wanna
Quit?' in the first TextBox & clicks the Button, the confirm dialog
asks the question 'Wanna Quit?'. The second TextBox displays true or
false when 'OK' or 'Cancel' is clicked in the confirm dialog
respectively.

Please give your frank & honest opinion on this. To be very honest,
this is the first time I have created a custom control in ASP.NET &
hence would like to get some feedback from others - be it positive or
negative!

All are welcome to comment.

Eliyahu Goldin wrote:
If you don't return "false", the event will arrive to the server. Now you
need to pass the user's choice in a hidden input control:

btnClick.Attrib utes.Add("OnCli ck",
"javascript:myF orm.inhConfirmR esponse.value = confirm('Are you sure you wish
to exit?') ? 'true' : 'false'");

and in the aspx page:

<input type="hidden" id="inhConfirmR esponse" runat="server" />

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
<rn**@rediffmai l.comwrote in message
news:11******** *************@e 3g2000cwe.googl egroups.com...
>>If Cancel is
clicked, the event won't propagate to the server
But I want the event to propogate so that ASP.NET can invoke a
different sub-routine.

As already said, I want the confirm dialog to come up when a Button is
clicked. The code snippet I showed in post #1 does this. Now I want to
invoke a sub-routine when the user clicks 'OK' which can be easily done
using the OnClick event of the Button control using the following code
snippet (assume that this ASPX page is named 'Proceed.aspx')

<script runat="server">
Sub Page_Load(..... )
btnClick.Attrib utes.Add("OnCli ck", "javascript:ret urn
confirm('Are you sure you wish to exit?');")
End Sub

Sub OKClicked(obj As Object, ea As EventArgs)
Response.Write( "You clicked OK")
End Sub
</script>
<form runat="server">
<asp:Button ID="btnClick" OnClick="OKClic ked" Text="PROCEED"
runat="server"/>
</form>

But if the user clicks 'Cancel' in the confirm dialog, I want ANOTHER
sub-routine named, say, 'CancelClicked' to be invoked. How do I invoke
the sub 'CancelClicked' when the user clicks 'Cancel' in the confirm
dialog? This is where I am getting stuck.

Actually prior to coming to this ASPX page named 'Proceed.aspx' that
has the above Button control, a few records entered by a user in
different Form fields get inserted in a temporary SQL Server 2005 DB
table. A user clicks this Button control; the JavaScript confirm dialog
with 'OK' & 'Cancel' buttons pops-up. If the user clicks 'OK', records
in the temporary table will be transferred to another DB table after
which, the records in the temporary table will get deleted. However, if
the user clicks 'Cancel' in the confirm dialog, then WITHOUT
transferring the records from the temporary table to the other DB
table, the records in the temporary table should get deleted.

That's precisely the reason why I want to execute a sub-routine even
when 'Cancel' in the confirm dialog is clicked.
Eliyahu Goldin wrote:
Are you asking on the server side? Your code already does it. If Cancel
is
clicked, the event won't propagate to the server.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
<rn**@rediffmai l.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
I have gone through a no. of posts in this NewsGroup regarding my
problem but alas, couldn't come across one which would have helped me
in resolving the issue. My problem is this:

An ASPX Form has a Button. When the Button is clicked, I want a
JavaScript confirm dialog to pop-up with the options 'OK' & 'Cancel'. I
have done this using the following code:

Sub Page_Load(....)
btnClick.Attrib utes.Add("OnCli ck", "javascript:ret urn confirm('Are
you sure you want to exit?');")
End Sub

where 'btnClick' is the name of the Button.

The problem is how do I find out which of the 2 buttons, 'OK' or
'Cancel', has a user clicked? Any one can help me out with this?

My primary intention is if the user clicks 'OK' in the confirm dialog,
then function1 should get executed but if the user clicks 'Cancel',
then function2 should get executed.

In JavaScript, if 'OK' is clicked, then confiorm returns 1; if 'Cancel'
is clicked, then confirm returns 0.
Oct 26 '06 #5
<rn**@rediffmai l.comwrote in message
news:11******** **************@ k70g2000cwa.goo glegroups.com.. .
Please give your frank & honest opinion on this. To be very honest,
this is the first time I have created a custom control in ASP.NET &
hence would like to get some feedback from others - be it positive or
negative!

All are welcome to comment.
Not wishing to be discouraging, but it seems to me that you have gone
through a hugely tortuous and convoluted route to achieve something so
incredibly simple...
Oct 26 '06 #6
Not wishing to be discouraging

Absolutely no problems at all....critics are a must for improvement...
>but it seems to me that you have gone
through a hugely tortuous and convoluted route to achieve something so
incredibly simple...
You are very much correct. In fact, I had implemented exactly the same
functionality using just a few simple lines about a fortnight back. I
just wanted to get a hang of custom controls......t hat's it....
Mark Rae wrote:
<rn**@rediffmai l.comwrote in message
news:11******** **************@ k70g2000cwa.goo glegroups.com.. .
Please give your frank & honest opinion on this. To be very honest,
this is the first time I have created a custom control in ASP.NET &
hence would like to get some feedback from others - be it positive or
negative!

All are welcome to comment.

Not wishing to be discouraging, but it seems to me that you have gone
through a hugely tortuous and convoluted route to achieve something so
incredibly simple...
Oct 26 '06 #7
BTW, Mark, could you please tell me what does the 'Protected' keyword
in the class file do?

Mark Rae wrote:
<rn**@rediffmai l.comwrote in message
news:11******** **************@ k70g2000cwa.goo glegroups.com.. .
Please give your frank & honest opinion on this. To be very honest,
this is the first time I have created a custom control in ASP.NET &
hence would like to get some feedback from others - be it positive or
negative!

All are welcome to comment.

Not wishing to be discouraging, but it seems to me that you have gone
through a hugely tortuous and convoluted route to achieve something so
incredibly simple...
Oct 26 '06 #8
<rn**@rediffmai l.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
BTW, Mark, could you please tell me what does the 'Protected' keyword
in the class file do?
http://msdn2.microsoft.com/en-us/library/ms173121.aspx
Oct 26 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2658
by: Muhammad Abdullah | last post by:
Hi am having some problems with the javascript confirm. i have it working fine on one page and it doesnt even pop up at the other. The code on the working page is, private void Page_Load(object sender, System.EventArgs e) { if(! Page.IsPostBack)
9
4929
by: tshad | last post by:
This is from my previous post, but a different issue. I have the following Javascript routine that opens a popup page, but doesn't seem to work if called from an asp.net button. It seems to work fine from a link. The button does bring up the popup window, but when I press the links on the page, it doesn't return or close the window. ****************************************************************************
7
2033
by: TJS | last post by:
javascript "confirm" fires after deletion instead of before deletion. how do I get this to stop the processing ? code ================== Sub ShowAlert(ByVal s As string) RegisterClientScriptBlock("CSP-showconfirm-function", _ "<s"+"cript language=""JavaScript"">" & vbCrLf & _
12
2145
by: tshad | last post by:
I have the following code that attaches a Javascript confirm box to my checkbox. When I select the checkbox, the window comes up fine, but it never executes the XfertoDefault_Click function when I press the Yes button. If I take off the Javascript event, the checkbox works fine. XferToDefault.Attributes.Add("onclick", "return confirm('Are you sure you want copy these entries?');") <asp:CheckBox ID="XferToDefault" AutoPostBack="true"...
3
4524
by: TJ | last post by:
Hi, I would like to ask something to user after user submits the form... I know I can ask user whehter they want to submit the form or not using client-script code such as onClick or onSubmit by adding into attributes collections... My problem is a little bit different than that... I would like to ask user something after submission is done
8
33721
by: Dave | last post by:
Hello all, I have been able to use a Javascript alert box in my vb.net web application, for example: Dim msg As String = "Hello, world." Dim alertScript As String = "<script language=JavaScript runat=server>" alertScript &= "alert('" & msg & "');" alertScript &= "</script>"
4
10030
by: tfsmag | last post by:
Okay, I have a project management app i'm writing and in the left hand menu i have a treeview control that is populated with each project... in child nodes under each project node I have an "edit" and a "delete" node. I have the functionality of the edit and delete working fine, my issue is now however that I cannot seem to find a way to add an "onclick" event that fires a confirm box. I've tried this method.. treenode.Text = "<a...
6
2532
by: den 2005 | last post by:
Hi everybody, Question 1: How do you set the values from server-side to a client-side control or how do you execute a javascript function without a button click event? Question 2: How do you get response from a Confirm() popup window to uncheck all server-side checkboxes placed in a panle of a web user control? I am using ASP.Net 2.0, Thanks. Need info...
1
108806
Frinavale
by: Frinavale | last post by:
Introduction I've seen many questions asked about how to disable the browser's back button and in the past I've replied with "it's simply not possible". It's not a good idea to disable the back button anyways, if the user ventures away from your page then they wouldn't have this button at their disposal. The main reason people ask how to control or disable the back button is because they have a need to control sensitive (and/or) dynamic web...
0
9916
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11056
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10782
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9531
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7932
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7093
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5761
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4174
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.