Last Updated: 14th March 2023
JavaScript functions can be registered and called dynamically from server side applications like Asp.Net. Here in this article I'll show you how to call a JavaScript function using a code behind procedure in Asp.Net (C# and VB).I am assuming that you have basic knowledge of JavaScript Statements and Code blocks.
The examples that I am going to show you here, covers three very popular Asp.Net code behind procedures to write Client Scripts.
The code behind procedures that I will be using in the example are,
01) RegisterClientScriptBlock – Register a block of Script without the "<script>" tags.
02) IsStartupScriptRegistered – Returns a Boolean (true or false), when a particular start up script is registered.
03) RegisterStartupScript – Register the Client Script from Code Behind.
<body> <form id="form1" runat="server"> <div> <!--In-addition, I have a button control, which when clicked will call a JS function registered using Asp.Net.--> <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>
The JavaScript function that I am going to "register and call" using code behind procedure, is the one I have declared inside the <script> tag in the header section. The function is 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 yet. In fact, I'll register the function using code behind procedure.
👉 The fact is, I am going to call and execute two JavaScript functions. The first script is at the client side, and the other script I have registered at the server side of the application.
Now see the code behind procedure.
using System; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // This is a first JS script, which will be executed when the page loads. Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Server Time", "window.onload = function () { script_CalledFrom_CodeBehind('" + DateTime.Now.TimeOfDay + "'); }", true); // This is the second JS script, which will be called when the button (see the markup) is clicked. if (!(Page.ClientScript.IsStartupScriptRegistered("Registered Script"))) First, check if script is registered. { string sScript = ""; sScript = sScript + "function startup() {"; sScript = sScript + "alert('Calling another script on Button Click event." + "This script is registered at Code Behind.');"; sScript = sScript + "}"; // Register now. Page.ClientScript.RegisterStartupScript(this.GetType(), "Registered Script", sScript, 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 ' This is a first JS script, which will be executed when the page loads. Page.ClientScript.RegisterClientScriptBlock _ (Me.GetType, "Server Time", "window.onload = function () {script_CalledFrom_CodeBehind('" & TimeOfDay & "');}", True) ' This is the second JS script, which will be called when the button (see the markup) is clicked. If Not (Page.ClientScript.IsStartupScriptRegistered("Registered Script")) Then Dim sScript As String = "" sScript = sScript & "function startup() {" sScript = sScript & "alert('Calling another script on Button Click event." & _ "This script is registered at Code Behind.');" sScript = sScript & "}" ' Now register. Page.ClientScript.RegisterStartupScript(Me.GetType, "Registered Script", sScript, True) End If End If End Sub End Class