In this article I will explain with an example, how to use the ClientScript.RegisterClientScriptBlock function for display JavaScript Alert Message Box from Server Side (Code Behind) in ASP.Net using C# and VB.Net.
This article will explain how to use the ClientScript.RegisterClientScriptBlock function for displaying JavaScript Alert Message Box on:
1. Page Load event.
2. Button Click event.
 
 
Display MessageBox using ClientScript.RegisterClientScriptBlock in Page Load
Inside the Page Load event of the page, the following 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.
string script = "<script type = 'text/javascript'>alert('Hello');</script>";
 
C#
protected void Page_Load(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 Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    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 MessageBox using ClientScript.RegisterClientScriptBlock on Button Click
Inside the Button Click event, the following 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 Button1_Click(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 Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    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
 
 
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.