In your C# web application, a common task is adding items to a database. But often just as important is the ability to delete items! The first method to accomplish this goal is linking an event to a LinkButton. This event links to a method that deletes. However, because a delete operation is a times permanent change, it is a good idea to force a user to confirm the decision to delete an item. This is to protect against accidental clicks.

Javascript Confirm on LinkButton Screenshot
As shown in the screenshot above, when a user clicks on a LinkButton, a Javascript confirm dialog box will appear with two buttons. Clicking OK will tell the method to continue and fire the event while clicking Cancel will prevent the event for firing. In this example, our text shows as “Are you sure?“, which is completely customizable to any text.
Via the LinkButton’s Properties Window
This is a quick and easy change to do via the LinkButton’s Property Window, so here’s how to do it!

Javascript Confirm on LinkButton
- Goto the LinkButton’s properties window.
- Find the OnClientClick attribute.
- Add the following: return confirm(‘Are you sure?’);
Programmatically
Even though changing this via the properties window is easy, there are still those who prefer to programmatically make the change. Here’s the code below for the example in our screenshot.
<asp:LinkButton ID="lbJavascriptConfirm" runat="server"
onclientclick="return confirm('Are you sure?');">Delete</asp:LinkButton>
And now this is how to make the change both programmatically and via a LinkButton’s property window. Please remember that this method will require that the web user has Javascript enabled. Additionally, the example uses this in a LinkButton, but any ASP Web Control that has the OnClientClick property.
RSS Feed
Posted in
Tags: 
