Last Updated: 30th June 2025
Learn how to dynamically call JavaScript functions from server-side applications using ASP.NET. In this tutorial, we'll demonstrate how to invoke JavaScript functions from code-behind in both C# and VB.NET, making your web applications more interactive and responsive.👉 This guide assumes you already have a foundational understanding of JavaScript statements and code blocks.
In this tutorial, we'll explore three widely-used ASP.NET code-behind methods for injecting client-side scripts. These examples will demonstrate how to effectively use:
01) RegisterClientScriptBlock – Register a block of Script without the "<script>" tags.
02) IsStartupScriptRegistered – Checks if a specific startup script is already registered, returning a Boolean result (true or false).
03) RegisterStartupScript – Registers client-side script code from the server-side code-behind.
The JavaScript function I’m registering and calling from the ASP.NET code-behind is defined within the <script> tag in the page's header section. This function is named script_CalledFrom_CodeBehind().
In addition, I have added a Button control in the body section. The button’s "click event" will call another function named startup(). If you look carefully the markup and script, I have not declared the "startup()" function anywhere. I'll register the function using code behind procedure.
<body>
<form id="form1" runat="server">
<div>
<!--I have a button control, which when clicked will call a JS function registered using code behind procedure.-->
<input type="button" value="Click it" onclick="startup()" />
</div>
</form>
</body>
<script>
// The script that will be called from code behind when the page loads.
function script_CalledFrom_CodeBehind(servertime) {
alert('Current Server Time: ' + servertime); // Alert server time.
}
</script>👉 In this example, I’ll be executing two JavaScript functions, one defined directly on the client side, and the other dynamically registered from the server-side code-behind in ASP.NET.
using System;
public partial class _Default : System.Web.UI.Page
{
protected void form1_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Register the first JavaScript function to be executed on page load.
string loadScript = "window.onload = function() {" +
"script_CalledFrom_CodeBehind('" + DateTime.Now.ToString("T") + "');" +
"};";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ServerTimeScript", loadScript, true);
// Check if the second script is already registered.
if (!Page.ClientScript.IsStartupScriptRegistered("StartupScript"))
{
// Define the JavaScript function to run on button click.
System.Text.StringBuilder startupScript = new System.Text.StringBuilder();
startupScript.Append("function startup() {");
startupScript.Append("alert('Calling another script on Button Click event. " +
"This script is registered at Code Behind.');");
startupScript.Append("}");
// Register the startup script.
Page.ClientScript.RegisterStartupScript(this.GetType(), "StartupScript", startupScript.ToString(), true);
}
}
}
}
Option Explicit On
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
If Not IsPostBack Then
' Register the first JavaScript function to be executed on page load.
Page.ClientScript.RegisterClientScriptBlock _
(Me.GetType, "Server Time", "window.onload = function() {" &
"script_CalledFrom_CodeBehind('" & TimeOfDay & "');" &
"};", True)
' Check if the second script is already registered.
If Not (Page.ClientScript.IsStartupScriptRegistered("Registered Script")) Then
' Define the JavaScript function to run on button click.
Dim startupScript As New Text.StringBuilder()
startupScript.Append("function startup() {")
startupScript.Append("alert('Calling another script on Button Click event. " &
"This script is registered at Code Behind.');")
startupScript.Append("}")
' Register the startup script.
Page.ClientScript.RegisterStartupScript(Me.GetType(), "StartupScript", startupScript.ToString(), True)
End If
End If
End Sub
End Class