The Old Confirm Button Trick
You have probably at some point clicked the wrong input button on a web page, and something bad happened.
You deleted the wrong user, submitted to the wrong newsletter or something similar. Wouldn’t it have been nice if you had been asked to confirm a critical decision like that? In ASP.NET it is so easy to create functionality around post backs caused by input buttons or similar click-controls. This is also the reason why it is important to let the user confirm actions to avoid mistakes.
If you press the OK button, the postback continues processing whatever server-side functionality you have implemented. If you press Cancel, nothing happens and the action has indeed been canceled.
To make it work, your have to use a regular button control and add an onclick attribute to it on runtime.
This is how it is done in ASP.NET 1.x
<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
&nbps;&nbps;&nbps; btnConfirm.Attributes.Add("onclick", "return confirm('Are you sure you want to click the button?');");
<asp:Button Runat="server" ID="btnConfirm" Text="Confirm" />
And the much easier way in ASP.NET 2.0
<asp:Button Runat="server" ID="btnConfirm" Text="Confirm" OnClientClick="return confirm('Are you sure you want to click the button?');" />
And finally the old fashion HTML way
<input type="submit" value="Confirm" onclick="return confirm('Are you sure you want to click the button?');" />
There is nothing fancy about this little trick, but it may help you do it a little easier. I for one could have used this trick a couple of years ago. Enjoy.
Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.