Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have generated buttons dynamically in the C# code behind for an asp.net project. I have written a event handler for each dynamically generated button. I need prompt a confirmation message from the code behind as it is a delete operation.

What I have tried:

ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "confirm('Do you Want to Delete ? ');", true);


how to get the return value, the user's choice either "ok" or "cancel" ?
Posted
Updated 30-Mar-17 22:27pm

What you're showing is not a confirmation from the code behind, but javascript. You're just writing it on the server, but it executes on the client.

Make a function that will be called on client button click (there is a property on Aspx Button control OnClientClick. In that function write something like:

return confirm("Are you sure you want to delete?");


That way, Yes will return true and you'll get code behind event. No will return false and the server trip (and event will not happen).

The advantage of this is that you can have the function written on the page or in a file somewhere and you don't need to register the script, just to set ClientClick property to "return functionName();"
 
Share this answer
 
v2
Comments
jkkr 31-Mar-17 2:06am    
but i have generated the buttons dynamically.... so i dont know how add OnClientControl properly
Richard Deeming 31-Mar-17 9:28am    
If you're using the Button control, set the OnClientClick property[^].

If you're generating plain HTML, set the onclick attribute, or use Javascript to wire up a handler to the "click" event.

If you're stuck, update your question with the relevant parts of the button generation code.
jkkr 1-Apr-17 0:13am    
Thanks @Sinisa and @Richard !!! solved the problem ! Using the OnClientClick property in the code behind !!

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