In this article I will explain with an example, how to write JavaScript function in Code Behind (Server Side) in ASP.Net using C# and VB.Net.
The JavaScript function will be written and executed using RegisterClientScriptBlock function of ClientScript class in ASP.Net using C# and VB.Net.
 
 
Display Message when user visits the page
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 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.
 
Notification Message on Form Submission
Inside the Page Load event of the page, the following JavaScript function will be registered using the RegisterOnSubmitStatement function. The below JavaScript function will display a message to the user when the user submits the Form using JavaScript Alert Message Box.
C#
protected void Page_Load(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 Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    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
 
Confirmation Message on Form Submission
Inside the Page Load event of the page, the following 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 Page_Load(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 Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    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 Message Box 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.