In this article I will explain with an example, how to display Message Boxes in ASP.Net using JavaScript. With the use of JavaScript alert we can display messages on different types of events in an ASP.Net Web Application.
 
 

Display Message when user visits the page

HTML Markup

Button – For show alert message when user visit the page.
<asp:Button ID="btnAlert" Text="Button" runat="server" OnClick="ShowAlert" />
 
 

Code Behind

Inside the VisitBtn event handler, the JavaScript function will be registered using the RegisterClientScriptBlock function. The below JavaScript function will display a greeting message to the user when the user visits the page using JavaScript Alert Message Box.
Note: The JavaScript Alert function is called inside the window.onload function so that it is displayed after the loading of the Page in Browser.
 
If the JavaScript function is called directly (as shown below), though it will display the message successfully but the message will be displayed before the page is loaded completely in Browser and the execution will be stopped until user closes the JavaScript Alert Message Box.
C#
protected void ShowAlert(object sender, EventArgs e)
{
    string message = "Hello! Mudassar.";
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("<script type = 'text/javascript'>");
    sb.Append("window.onload=function(){");
    sb.Append("alert('");
    sb.Append(message);
    sb.Append("')};");
    sb.Append("</script>");
    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
}
 
VB.Net
Protected Sub ShowAlert(sender As Object, e As EventArgs)
    Dim message As String "Hello! Mudassar."
    Dim sb As New System.Text.StringBuilder()
    sb.Append("<script type = 'text/javascript'>")
    sb.Append("window.onload=function(){")
    sb.Append("alert('")
    sb.Append(message)
    sb.Append("')};")
    sb.Append("</script>")
    ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", sb.ToString())
End Sub
 
Display Alert Message in ASP.Net from code behind using C# and VB.Net
 
 

Display Message when user submits the page

There are two types of messages which can be displayed when the User submits the Form.
1. Notification message.
2. Confirmation message.
 
1. HTML Markup
Button – For show alert (Notification) message.
<asp:Button ID="btnNotification" Text="Notification" runat="server" OnClick="ShowNotification" />
 

Notification Message on Form Submission

Inside the ShowNotification event handler, the JavaScript function will be registered using the RegisterOnSubmitStatement function. The below JavaScript function will display a message to the user of Notification on Form Submission, the Form using JavaScript Alert Message Box.
C#
protected void ShowNotification(object sender, EventArgs e)
{
    string message = "Your request is being processed.";
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("alert('");
    sb.Append(message);
    sb.Append("');");
    ClientScript.RegisterOnSubmitStatement(this.GetType(), "alert", sb.ToString());
}
 
VB.Net
Protected Sub ShowNotification(sender As Object, e As EventArgs)
    Dim message As String "Your request is being processed."
    Dim sb As New System.Text.StringBuilder()
    sb.Append("alert('")
    sb.Append(message)
    sb.Append("');")
    ClientScript.RegisterOnSubmitStatement(Me.GetType(), "alert", sb.ToString())
End Sub
 
Display Alert Message in ASP.Net from code behind using C# and VB.Net
 
2. HTML Markup
Button – For show alert (Confirmation) message.
<asp:Button ID="btnConfirmation" Text="Confirmation" runat="server" OnClick="ShowConfirmation" />
 

Confirmation Message on Form Submission

Inside the ShowConfirmation event handler, the JavaScript function will be registered using the RegisterOnSubmitStatement function. The below JavaScript function will display a JavaScript Confirmation Message Box to the user when the user submits the Form.
C#
protected void ShowConfirmation(object sender, EventArgs e)
{
    string message = "Do you want to Submit?";
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("return confirm('");
    sb.Append(message);
    sb.Append("');");
    ClientScript.RegisterOnSubmitStatement(this.GetType(), "alert", sb.ToString());
}
 
VB.Net
Protected Sub ShowConfirmation(sender As Object, e As EventArgs)
    Dim message As String "Do you want to Submit?"
    Dim sb As New System.Text.StringBuilder()
    sb.Append("return confirm('")
    sb.Append(message)
    sb.Append("');")
    ClientScript.RegisterOnSubmitStatement(Me.GetType(), "alert", sb.ToString())
End Sub
 
Display Alert Message in ASP.Net from code behind using C# and VB.Net
 
 

Display Message Box on Button Click

HTML Markup

Button – For show alert message when user click Button.
<asp:Button ID="btnOrderPlace" Text="OrderPlace" runat="server" OnClick="ShowOrderPlace" />
 
 

Code Behind

Inside the Button Click event handler, the JavaScript function will be registered using the RegisterClientScriptBlock function. The below JavaScript function will display a JavaScript Alert Message Box to the user when he clicks a Button. Example, displaying a message to the user saying that “Email Sent Successfully” or “Order Placed Successfully”.
C#
protected void ShowOrderPlace(object sender, EventArgs e)
{
    string message = "Order Placed Successfully.";
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("<script type = 'text/javascript'>");
    sb.Append("window.onload=function(){");
    sb.Append("alert('");
    sb.Append(message);
    sb.Append("')};");
    sb.Append("</script>");
    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
}
 
VB.Net
Protected Sub ShowOrderPlace(sender As Object, e As EventArgs)
    Dim message As String "Order Placed Successfully."
    Dim sb As New System.Text.StringBuilder()
    sb.Append("<script type = 'text/javascript'>")
    sb.Append("window.onload=function(){")
    sb.Append("alert('")
    sb.Append(message)
    sb.Append("')};")
    sb.Append("</script>")
    ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", sb.ToString())
End Sub
 
Display Alert Message in ASP.Net from code behind using C# and VB.Net
 
 

Browser Compatibility

The above code has been tested in the following browsers.
Microsoft Edge   FireFox  Chrome  Safari  Opera
* All browser logos displayed above are property of their respective owners.
 
 

Downloads