Inserire un “confirm messagebox” ad un Asp.net Button control

A my colleague asked my a way to create a confirm button in Asp.net. This is the solution:

The Asp.Net button has 2 attributes to trigger a click event OnClick and OnClientClick.

OnClientClick – Calls the client side (JavaScript) event handler
OnClick– Calls the server side (code-behind) event handler

The OnClientClick event handler can be used to perform client side validations and to get a confirmation from the user before triggering the server side event handler as follows.

OnClientClick="if(!confirm('Are you sure you want to Save?')) return false;"
<html>
<head runat="server">
    <title>OnClientClick</title>
</head>
<body>
    <form id="frmOnClientClick" runat="server">
    <div>
        <asp:Button
            id="btnSave"
            runat="server"
            Text="Client Click"
            OnClientClick="if(!confirm('Are you sure you want to Save?'))  
            return false;"
            onclick="btnSave_Click"/>
    </div>
    </form>
</body>
</html>

What do you think?

One thought on “Inserire un “confirm messagebox” ad un Asp.net Button control

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...