I have a button on my asp.net site which activates javascript through OnClientClick property which is set to potwierdzenie().
The script:
<script type="text/javascript">
function potwierdzenie()
{
var answer = confirm("Do you want to delete it")
if (answer)
???
else
???
}
</script>
All I want to do is do some work (deleting something from mysql db) in c# depending on what user clicked (yes or cancel). How can I do this? How can I execute c# code in this script or how can I do it the other easiest way?
Just return true or false from this callback:
Now if the user selects Yes, true will be returned. And if true is returned ASP.NET will execute the OnClick server side callback to which you have subscribed and perform the actual action:
If the user selects No, false will be returned and the server side callback will never be called.
It’s important though that in your OnClientClick subscription you do this:
And here’s a full example of how your link might look like: