In this article I will explain with an example, how to use the ClientScript.RegisterOnSubmitStatement function for displaying MessageBox from Server-Side (Code-Behind) in ASP.Net using C# and VB.Net.
This article will explain how to use the ClientScript.RegisterOnSubmitStatement function for displaying:
1. JavaScript Alert MessageBox on Page Load event.
2. JavaScript Confirmation Box on Button Click event.
 
 

Displaying MessageBox using ClientScript.RegisterOnSubmitStatement in Page Load

Inside the Page_Load event handler, the following JavaScript function will be registered using the RegisterOnSubmitStatement function.
Registered JavaScript function will look like as shown below on Source page
Using ClientScript.RegisterOnSubmitStatement in ASP.Net
 
The below JavaScript function will display a message to the user when the user submits the Form.
C#
protected void Page_Load(object sender, EventArgs e)
{
    string message = "Form has been submitted.";
    StringBuilder sb = new StringBuilder();
    sb.Append("alert('");
    sb.Append(message);
    sb.Append("');");
    ClientScript.RegisterOnSubmitStatement(this.GetType(), "alert", sb.ToString());
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim message As String = "Form has been submitted."
    Dim sb As StringBuilder = New StringBuilder()
    sb.Append("alert('")
    sb.Append(message)
    sb.Append("');")
    ClientScript.RegisterOnSubmitStatement(Me.GetType(), "alert", sb.ToString())
End Sub
 
 

Screenshot

Using ClientScript.RegisterOnSubmitStatement in ASP.Net
 
 

Displaying Confirmation Box using ClientScript.RegisterOnSubmitStatement on Button Click

Inside the Page_Load event handler, the following JavaScript function will be registered using the RegisterOnSubmitStatement function.
Registered JavaScript function will look like as shown below on Source page
Using ClientScript.RegisterOnSubmitStatement in ASP.Net
 
The below JavaScript function will display a JavaScript Confirmation MessageBox to the user when user submits the Form.
C#
protected void Page_Load(object sender, EventArgs e)
{
    string message = "Do you want to Submit?";
    StringBuilder sb = new StringBuilder();
    sb.Append("return confirm('");
    sb.Append(message);
    sb.Append("');");
    ClientScript.RegisterOnSubmitStatement(this.GetType(), "alert", sb.ToString());
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim message As String = "Do you want to Submit?"
    Dim sb As StringBuilder = New StringBuilder()
    sb.Append("return confirm('")
    sb.Append(message)
    sb.Append("');")
    ClientScript.RegisterOnSubmitStatement(Me.GetType(), "alert", sb.ToString())
End Sub
 
 

Screenshot

 
 Using ClientScript.RegisterOnSubmitStatement in ASP.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.