How to Call JavaScript Function from Code behind in Asp.Net C# Vb.Net

In the implementation time we need to call Javascript function from code behind or server side. Don’t think it’s a complex job. It’s really an easy job to Call a Javascript function from C# VB.Net code. To call javascript function from code-behind, we need to use ScriptManager.RegisterStartupScript() method.

Objective/Outcome:
How to Call JavaScript Function from Code behind in Asp.Net C# Vb.Net

Code Syntax of ScriptManager.RegisterStartupScript() method:

public void RegisterStartupScript(
	Type type,
	string key,
	string script,
	bool addScriptTags
)

Parameters Explanation:
Type:
The type of the startup script to register.

Key:
The key of the startup script to register.

Script:
The startup script literal to register.

AddScriptTags:
A Boolean value indicating whether to add script tags.

To do the Example Prepare the HTML like below:

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
    <asp:Button ID="Button1" runat="server" Text="Call Javascript Function" onclick="Button1_Click" />
    <asp:Button ID="Button2" runat="server" Text="Call Javascript Function with Script Block" onclick="Button2_Click"/>

Write the below Javascript Function in your page:

<script type="text/javascript">
    function CheckJavascriptFunction() {
        //Write your javascript client side code here
        //alert('Invoked from C# VB.Net Server Side');
        alert(document.getElementById("<%=TextBox1.ClientID%>").value);
    }
</script>

Now Call Javascript Function From Code Behindor Server Side:
C# Code:

    protected void Button1_Click(object sender, EventArgs e)
    {
        System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "check_Javascript", "CheckJavascriptFunction();", true);
    }

VB.Net Code:

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        System.Web.UI.ScriptManager.RegisterStartupScript(Me, [GetType](), "check_Javascript", "CheckJavascriptFunction();", True)
    End Sub

Now Call Javascript Function From Code Behind or Server Side with add Script Tags:
C# Code:

    protected void Button2_Click(object sender, EventArgs e)
    {
        System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "check_Javascript", "<script type='Text/Javascript'>CheckJavascriptFunction();</script>", false);
    }

VB.Net Code:

    Protected Sub Button2_Click(sender As Object, e As EventArgs)
        System.Web.UI.ScriptManager.RegisterStartupScript(Me, [GetType](), "check_Javascript", "<script type='Text/Javascript'>CheckJavascriptFunction();</script>", False)
    End Sub

Note:
1. If you look at the Syntax you will find a boolean parameter “addScriptTags”. If you make it true then you don’t need to wrap the Javascript code by script block. Otherwise you have to provide.

2. The script block added by the RegisterStartupScript method executes when the page finishes loading but before the page’s OnLoad event is raised.

3. The script blocks are not guaranteed to be output in the order they are registered. If the order of the script blocks is important, use a StringBuilder object to gather the scripts together in a single string, and then register them all in a single client script block.

Ref: MSISDN

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, Client, Javascript, VB.Net
2 comments on “How to Call JavaScript Function from Code behind in Asp.Net C# Vb.Net
  1. John Alexander says:

    I downloaded the code example in vb.net but it isn\’t readable as it is rar format and I don\’t know what that is

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*