Run server code on a javascript confirm

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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
 
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]
 
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
 
Hello louise,

return false will cancel your postback...

so...

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

should do the trick.
 
Back
Top