By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,078 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,078 IT Pros & Developers. It's quick & easy.

JavaScript confirm

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
>>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

P: n/a
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

P: n/a
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

P: n/a
<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

P: n/a
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

P: n/a
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

P: n/a
<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 discussion thread is closed

Replies have been disabled for this discussion.