How do I display alert & confirmation dialogs in Web Forms?

Displaying an Alert Dialog

The following code provides an example of code you can add to your Web Form's Load method to display an alert when a button named btnProcess is clicked:

In C#

this.btnProcess.Attributes.Add("onclick", "alert('This is an alert!');");

And in VB .NET:

Me.btnProcess.Attributes.Add("onclick", "alert('This is an alert!');")

When the user clicks the Process button at run time, the following alert dialog is displayed:

When the user clicks OK, the page gets posted back and the Process button's Click event handler gets executed.

Displaying a Confirmation Dialog

The following code provides an example of code you can add to your Web Form's Load method to display an alert when a button named btnDelete is clicked:

In C#:

this.btnDelete.Attributes.Add("onclick", "if(confirm('Are you sure to delete?')){}else{return false}");

And in VB .NET:

Me.btnDelete.Attributes.Add("onclick", "if(confirm('Are you sure to delete?')){}else{return false}")

When the user clicks the Delete button at run time, the following confirmation dialog is displayed:

If the user clicks OK, nothing happens. If they click Cancel, the page gets posted back and the Delete button's Click event handler gets executed.


© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 04/26/18
Comment or report problem with topic