Home Asp.net Call JavaScript Function from Code-behind in Asp.net C# Vb

Call JavaScript Function from Code-behind in Asp.net C# Vb

10
16

In my previous tutorials, I’d explained how to show alert message from javascript, how to show alert message from code-behind, confirm message box example using javascript and other more cracking tutorials on Asp.net, JavaScript, jQuery here.

Now here in this tutorial, I’ll explain how to call javascript function from code-behind or server-side in asp.net using c# or vb.net with example code.

To call javascript function from code-behind, we need to use ScriptManager.RegisterStartupScript() method. Here is the syntax for this method:

ScriptManager.RegisterStartupScript(Control control, Type type,string key, string script,
bool addScriptTags);

Call JavaScript Function from Code-behind or Server-side

Following is the complete HTML Markup code that I used in my .aspx page to call javascript function from code-behind:

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head id=”Head1″ runat=”server”>
<title>Call JavaScript function from code-behin in Asp.net</title>
<script type=”text/javascript”>
function myFunction() {
//some code here
alert(‘Function called successfully!’);
}
</script>
</head>
<body>
<form id=”form1″ runat=”server”>
<h4>
Call JavaScript function from code-behin in Asp.net</h4>
<div>
<asp:Button ID=”btnServerSide” runat=”server” OnClick=”btnServerSide_Click”
Text=”Call Function” />
</div>
</form>
</body>
</html>

As you can see from above code, I used OnClick event as OnClick=”btnServerSide_Click” to call javascript function from code-behind or server-side. Now define that event in code-behind as shown below.

Call JavaScript Function From Server-side In C#

//Following statement is used to call pre-defined javascript function
protected void btnServerSide_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), “myFunction”, “myFunction();”, true);
}

OR

If you are dealing with asp.net UpdatePanel and UpdateProgress, use the following code:

//Following statement is used to call pre-defined javascript function
protected void btnServerSide_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(myUpdatePanelID, myUpdatePanelID.GetType(),
“myFunction”, “myFunction();”, true);
}

Call JavaScript Function From Server-side In Vb.net

‘Following statement is used to call pre-defined javascript function
Protected Sub btnServerSide_Click(ByVal sender As Object, ByVal e As EventArgs)
ScriptManager.RegisterStartupScript(Me, Me.GetType(), “myFunction”, “myFunction();”, True)
End Sub

OR

If you are dealing with asp.net UpdatePanel and UpdateProgress, use the following code:

‘Following statement is used to call pre-defined javascript function
Protected Sub btnServerSide_Click(ByVal sender As Object, ByVal e As EventArgs)
ScriptManager.RegisterStartupScript(myUpdatePanelID, myUpdatePanelID.GetType(),
“myFunction”, “myFunction();”, True)
End Sub

Example Result

How to javascript function from code-behind in asp.net c# vb.net?

16 COMMENTS

  1. That’s really great but after I call my JavaScript function using RegisterStartupScript my UpdateProgress panel stops working. Does the RegisterStartupScript call damage the other JS that’s on the page… the JS that came with the ASP.NET controls? Any thoughts?

  2. I’m calling a JS fcn that fires AJAX to a webmethod and returns a boolean. How do I retrieve the return value in the codebehind? (VB) I can set a hiddenfield value in the JS, but if it’s async how can my VB know when it’s changed?

    • You can ofcource use hiddenfield as runat=”server” and update its value via jQuery and retrieve its updated values from server-side by using “hdnField.Value” (you will get string value so if you need bool then convert it to boolean) as VB code.

  3. Hi, thanks for the info.

    I need to get the return value from the js function, how do i do this in vb,net code behind?

  4. myFunction() is calling on each time when page refreshing and alert is displaying on each page load.
    It is happening after we use it first time. How to solve this problem ?

LEAVE A REPLY

Please enter your comment!
Please enter your name here
Captcha verification failed!
CAPTCHA user score failed. Please contact us!