Using javascript confirm in .aspx

D

Dave

I'm tearing my hair out over this one.

Using VS 2003, inline (not code-behind), vb.net.

I have a button (btnClear) that calls a vb routine that goes into sql server
and truncates a table.

What I want is to call a javascript confirm box upon clicking the button
that asks the user if he's sure he wants to do the deed. I have tried so
many permutations and combinations of RegisterClientScriptBlock, <input
onclick=, runat=server, btnClear.Attributes.Add("onclick",".....") that my
head is spinning.

I just can't seem to get the confirm return value to the vb.net code, the
server-side, or whatever the correct term is. After Googling for help, I
saw many examples of javascript code that is easy to understand and use, but
none that works together with vb.net code.

Can anyone provide a clear method of doing this?


Thanks in advance,

Dave
 
R

Richard Dudley

If you add this little JS snippet to your button: onClick="return
confirm('Are you sure you want to delete this report?');".

When the button is clicked, a Yes/No dialog should open. If the user clicks
No, then the script will return false, and nothing should happen. This is
automatic, and you don't need to check for the return value. If they click
Yes, then the button should fire.
 
D

Dave

Richard,

Thank you very much for your response. Your answer may be part of the
solution, but I still can't get it to do everything needed. First when you
say "If you add ...", I assume you mean something like this:

<td>
<input OnClick="return confirm('Are you sure you want to delete this
report?');"
type="button" value="Clear" runat="server" id="btnClearVisits">
</td>

I do ge the desired dialog box, but the desired code does not fire. My code
looks something like this:

Sub btnClearVisits(ByVal sender As System.Object, ByVal e As
System.EventArgs)
ClearVisits()
end sub

sub ClearVisits()

[...]

dim sql As String = "TRUNCATE TABLE tblVisits "
Dim NumAffected As Integer = cmd.ExecuteNonQuery()

[...]
end sub

Do you see anything obvious I'm missing or doing wrong? It appears the sub
ClearVisits() never executes. Before I got the idea of confirming with the
user, the sub ClearVisits() worked fine.

Thanks again.

Dave
 
G

Guest

If you want the 'intended' way rather than having to fudge it, have you
considered whether any of the '~Validator' objects will work? Don't know
whether they will suit your purpose but have a look at this link
http://aspnet.4guysfromrolla.com/articles/073102-1.aspx
this is Scott Mitchell's website, basically anything I've ever wanted to
know about asp.net I've managed to find there.

alternatively one more code line to try:
<td>
<input OnClick="if(confirm('Are you sure you want to delete this
report?')) btnClearVisits_Click();"
type="button" value="Clear" runat="server" id="btnClearVisits">
</td>
might be way off but then again it might work...

Dave said:
Richard,

Thank you very much for your response. Your answer may be part of the
solution, but I still can't get it to do everything needed. First when you
say "If you add ...", I assume you mean something like this:

<td>
<input OnClick="return confirm('Are you sure you want to delete this
report?');"
type="button" value="Clear" runat="server" id="btnClearVisits">
</td>

I do ge the desired dialog box, but the desired code does not fire. My code
looks something like this:

Sub btnClearVisits(ByVal sender As System.Object, ByVal e As
System.EventArgs)
ClearVisits()
end sub

sub ClearVisits()

[...]

dim sql As String = "TRUNCATE TABLE tblVisits "
Dim NumAffected As Integer = cmd.ExecuteNonQuery()

[...]
end sub

Do you see anything obvious I'm missing or doing wrong? It appears the sub
ClearVisits() never executes. Before I got the idea of confirming with the
user, the sub ClearVisits() worked fine.

Thanks again.

Dave


Richard Dudley said:
If you add this little JS snippet to your button: onClick="return
confirm('Are you sure you want to delete this report?');".

When the button is clicked, a Yes/No dialog should open. If the user
clicks
No, then the script will return false, and nothing should happen. This is
automatic, and you don't need to check for the return value. If they
click
Yes, then the button should fire.
 
D

Dave

Thanks, Bonj, that's an interesting idea. I know a little about Validators,
but hadn't thought about using them here. One of my goals is to write more
platform-independent code, and so using a Validator control will be one of
the later solutions I try.

I'm still curious about solving this problem in a straightforward way,
because it will enlighten some concepts for me I need to know as a
programmer, and will be useful for the future. I think the answer is
simple, but you need to know more about the ins and outs of client-server
than I currently know.

Dave

Bonj said:
If you want the 'intended' way rather than having to fudge it, have you
considered whether any of the '~Validator' objects will work? Don't know
whether they will suit your purpose but have a look at this link
http://aspnet.4guysfromrolla.com/articles/073102-1.aspx
this is Scott Mitchell's website, basically anything I've ever wanted to
know about asp.net I've managed to find there.

alternatively one more code line to try:
<td>
<input OnClick="if(confirm('Are you sure you want to delete this
report?')) btnClearVisits_Click();"
type="button" value="Clear" runat="server" id="btnClearVisits">
</td>
might be way off but then again it might work...

Dave said:
Richard,

Thank you very much for your response. Your answer may be part of the
solution, but I still can't get it to do everything needed. First when
you
say "If you add ...", I assume you mean something like this:

<td>
<input OnClick="return confirm('Are you sure you want to delete this
report?');"
type="button" value="Clear" runat="server" id="btnClearVisits">
</td>

I do ge the desired dialog box, but the desired code does not fire. My
code
looks something like this:

Sub btnClearVisits(ByVal sender As System.Object, ByVal e As
System.EventArgs)
ClearVisits()
end sub

sub ClearVisits()

[...]

dim sql As String = "TRUNCATE TABLE tblVisits "
Dim NumAffected As Integer = cmd.ExecuteNonQuery()

[...]
end sub

Do you see anything obvious I'm missing or doing wrong? It appears the
sub
ClearVisits() never executes. Before I got the idea of confirming with
the
user, the sub ClearVisits() worked fine.

Thanks again.

Dave


Richard Dudley said:
If you add this little JS snippet to your button: onClick="return
confirm('Are you sure you want to delete this report?');".

When the button is clicked, a Yes/No dialog should open. If the user
clicks
No, then the script will return false, and nothing should happen. This
is
automatic, and you don't need to check for the return value. If they
click
Yes, then the button should fire.

I'm tearing my hair out over this one.

Using VS 2003, inline (not code-behind), vb.net.

I have a button (btnClear) that calls a vb routine that goes into sql
server
and truncates a table.

What I want is to call a javascript confirm box upon clicking the
button
that asks the user if he's sure he wants to do the deed. I have tried
so
many permutations and combinations of RegisterClientScriptBlock,
<input
onclick=, runat=server, btnClear.Attributes.Add("onclick",".....")
that
my
head is spinning.

I just can't seem to get the confirm return value to the vb.net code,
the
server-side, or whatever the correct term is. After Googling for
help, I
saw many examples of javascript code that is easy to understand and
use,
but
none that works together with vb.net code.

Can anyone provide a clear method of doing this?


Thanks in advance,

Dave
 
D

Dave

I added this - btnClearVisits.Attributes.add("OnClick","javascript:return
confirm('Are you sure you want to delete this report?');") - to my
Page_Load, removed it from the <input> tag, and it worked great after I
removed some CS101 errors I had put in previously.

I appreciate the responses to my problem.

Dave
 

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

Top