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:
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:
<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#
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:
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
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:
Protected Sub btnServerSide_Click(ByVal sender As Object, ByVal e As EventArgs)
ScriptManager.RegisterStartupScript(myUpdatePanelID, myUpdatePanelID.GetType(),
“myFunction”, “myFunction();”, True)
End Sub
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?
I think it should work even with “UpdatePanel” and “UpdateProgress”. If its not working then check the post again, I updated my post.
Let me know it works for you?
Still no joy. It seems that if I call the function when the UpdateProgress panel is open, the panel never closes.
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.
It’s asynchronous. How does my code behind know when to get the value without a postback?
You need to use update panel for asynchronous callbacks and also put that hiddenfield to update panel. So whenever you call asynchronous callbacks, your update panel also get updated.
The client side value change does not cause postback. nevermind, thanks for trying. I just replicated the webmethod in the code behind.
I also means same think by using update panel but in different approach. Nevermind, thanks for posting comments. 🙂
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?
Hi Xehanort,
As basis of this, you can’t get value directly in code-behind because your client-side code executes on client-side and server-side code executes server-side, those two parts of code never call one another, right?
But don’t worry, we have hiddenfield that do work for us. What you need to do is set hiddenfield value (using JavaScript/jQuery) and retrieve that value server-side.
Here is a workaround, you can check for the reference: http://www.codeproject.com/Questions/228944/Can-i-get-value-from-javascript-in-Csharp-code-beh
if javascript function is return type then how can call on btn click
and how we catch return value
Check my last reply to Xehanort.
Thanks For the information.. it was very important for me.
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 ?
What do you mean by displaying on each page load? The example referring to onclick event. Did you prepared jsfiddle or something to check what you’re doing?