473,416 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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.Attributes.Add("OnClick", "javascript:return 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 3997
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**@rediffmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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.Attributes.Add("OnClick", "javascript:return 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.Attributes.Add("OnClick", "javascript:return
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="OKClicked" 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**@rediffmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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.Attributes.Add("OnClick", "javascript:return 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.Attributes.Add("OnClick",
"javascript:myForm.inhConfirmResponse.value = confirm('Are you sure you wish
to exit?') ? 'true' : 'false'");

and in the aspx page:

<input type="hidden" id="inhConfirmResponse" runat="server" />

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
<rn**@rediffmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.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.Attributes.Add("OnClick", "javascript:return
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="OKClicked" 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**@rediffmail.comwrote in message
news:11**********************@b28g2000cwb.googleg roups.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.Attributes.Add("OnClick", "javascript:return 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.WebControls

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='JavaScript'>")
Output.Write(vbCrLf)
Output.Write("var answer = confirm('")
Output.Write(strConfirmMsg)
Output.Write("')")
Output.Write(vbCrLf)
Output.Write("document.frmCC.txtGetAnswer.value = answer")
Output.Write(vbCrLf)
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="Confirm" Assembly="ConfirmCC" %>

<script runat="server">
Sub ConfirmSub(ByVal obj As Object, ByVal ea As EventArgs)
ccConfirm.ConfirmMessage = txtMsg.Text
End Sub
</script>
<form id="frmCC" runat="server">
<asp:TextBox ID="txtMsg" runat="server"/>
<asp:TextBox ID="txtGetAnswer" runat="server"/>
<asp:Button ID="btnConfirm" OnClick="ConfirmSub" Text="CONFIRM"
runat="server"/>
<CC:ShowConfirm 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.Attributes.Add("OnClick",
"javascript:myForm.inhConfirmResponse.value = confirm('Are you sure you wish
to exit?') ? 'true' : 'false'");

and in the aspx page:

<input type="hidden" id="inhConfirmResponse" runat="server" />

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
<rn**@rediffmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.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.Attributes.Add("OnClick", "javascript:return
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="OKClicked" 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**@rediffmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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.Attributes.Add("OnClick", "javascript:return 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**@rediffmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.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......that's it....
Mark Rae wrote:
<rn**@rediffmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.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**@rediffmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.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**@rediffmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.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
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...
9
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...
7
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)...
12
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...
3
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...
8
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...
4
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"...
6
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...
1
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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...

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.