Click here to Skip to main content
16,014,940 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi all,

I have doubt in JavaScript that is how to write JavaScript in codebehind in vb.net.

Here I would like to write javascript confirm box and if user click on ok then delete querry will execute can any help me.


Thanks in Advance

Seshu
Posted
Updated 25-May-11 9:53am
v2

Button btn = new Button();
btn.Text = "Delete";
btn.Attributes["onclick"] = "javascript: if( confirm('Do you want to delete?') ) { delete(); } return false;";

Controls.Add(btn);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-May-11 15:28pm    
Pretty simple, isn't it? A 5.
--SA
similar to Mark's, but what I usually do it fire the "delete" functionality from the server.

Therefore, by modifying his JavaScript a smidge:

btn.Attributes["onclick"] = "javascript: return confirm('Do you want to delete?');";


By passing the confirm (which gives a true/false value as a result) directly to the onclick handler, if you return a cancel/false attribute, the onclick doesn't complete and the form submittal will not occur.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-May-11 15:28pm    
My 5.
--SA
[no name] 25-May-11 15:34pm    
Since the OP gave no details as to what the delete function was I didn't make any assumptions that a postback was necessary/desired.
Deepthi Aravind 26-May-11 7:32am    
My 5...
Good solution...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900