Run server code on a javascript confirm

G

Guest

I want to run some server code but before I do i want to ask the user if they
are sure using a javascript confirm. I'm confused as to how to do this. I can
build a javascript string then do
btnRemove.attributes.add(onclick)=javascript string, but then how do i say on
hitting ok carry out this server code(on hitting cancel do nothing). Can
someone help?

thanks
 
E

Eliyahu Goldin

Server code runs after submitting the form. You would need to call
myForm.submit(). Probably you would want to pass some info from client to
server in order to let server know what to do.

Eliyahu
 
K

Ken Cox [Microsoft MVP]

Hi Louise,

Try this?

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
btndeleteuser.Attributes.Add("onclick", _
"javascript:return confirm('Delete it?');")
End Sub

Private Sub btndeleteuser_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btndeleteuser.Click
Response.Write("Deleted")
End Sub

<asp:Button Text="Delete" class="label" ID="btndeleteuser" Runat="server"
/>

Ken
MVP [ASP.NET]
 
G

Guest

thanks ken I am using c# but i get the drift. something i didnt mention is
that the button is on an asp:repeater, therefore it gives the button a
generated ID, not btnRemove, so i'm going to have to work that out.

Thanks.

Ken Cox said:
Hi Louise,

Try this?

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
btndeleteuser.Attributes.Add("onclick", _
"javascript:return confirm('Delete it?');")
End Sub

Private Sub btndeleteuser_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btndeleteuser.Click
Response.Write("Deleted")
End Sub

<asp:Button Text="Delete" class="label" ID="btndeleteuser" Runat="server"
/>

Ken
MVP [ASP.NET]


louise raisbeck said:
I want to run some server code but before I do i want to ask the user if
they
are sure using a javascript confirm. I'm confused as to how to do this. I
can
build a javascript string then do
btnRemove.attributes.add(onclick)=javascript string, but then how do i say
on
hitting ok carry out this server code(on hitting cancel do nothing). Can
someone help?

thanks
 
M

Matt Berther

Hello louise,

return false will cancel your postback...

so...

btnRemove.Attributes.Add("onClick", "return confirm('Some Prompt');");

should do the trick.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top