Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling ASP.NET Code Behind function from JavaScript

Is it possible to call ASP.NET codebehind function from JavaScript.

like image 882
Satyaprakash J Avatar asked Mar 26 '10 05:03

Satyaprakash J


People also ask

Can we call code behind function from JavaScript?

The Code Behind function can be called from Client Side (JavaScript) using ASP.Net AJAX PageMethods in ASP.Net. The first thing you need to do is add a ASP.Net AJAX ScriptManager to the page and set its EnablePageMethods property to true.

Can you use .NET with JavaScript?

NET code can do virtually anything when combined with HTML and CSS but JavaScript is still very important and the reason lies in the different domains in which each language operates.

How do I call a JavaScript function with parameters from code behind C#?

In order to call the JavaScript function with parameter from Code Behind, one has to make use of the RegisterStartupScript method of the ClientScript class in ASP.Net using C# and VB.Net. The following HTML Markup consists of an ASP.Net Button and a Label control.


1 Answers

yes, you can make web method like..

<WebMethod(EnableSession:=True), ScriptMethod()> _ 
Public Shared Function updateContent() As String
      Return "Your String"
    End Function

and then call in javascript like..

PageMethods.updateTabContent(parameterValueIfAny, onSuccessMethod,onFailMethod);

this also need to add

<asp:ScriptManager ID="ScriptMgr" runat="server" EnablePageMethods="true">
                </asp:ScriptManager>
like image 67
Muhammad Akhtar Avatar answered Oct 11 '22 13:10

Muhammad Akhtar