How to call code behind function from javascript in asp.net
We can call page methods (static methods declared in scope of asp.net page) using MS Ajax framework for ASP.NET. That is how can we do it :
1) Drop ScriptManager control on page
2) Set EnablePageMethods property of ScriptManager control to true
3) Add static public method in page-behind code (or its parent class) and mark it with [System.Web.Services.WebMethod] attribute:
4) add following javascript code to it
We can call page methods (static methods declared in scope of asp.net page) using MS Ajax framework for ASP.NET. That is how can we do it :
1) Drop ScriptManager control on page
2) Set EnablePageMethods property of ScriptManager control to true
3) Add static public method in page-behind code (or its parent class) and mark it with [System.Web.Services.WebMethod] attribute:
4) add following javascript code to it
Calling a static "page method" from Javascript using MS AJAX
Atlas gave us the ability to easily call Web Services from JavaScript. MS AJAX has gone one step further! We can now call methods in the codebehine of the current page from Javascript. Here's how:
This is designed for v1.0.61025.0 of AJAXExtensionsToolbox.dll
Enable Page Methods on your ScriptManager
Set the EnablePageMethods attribute to true
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" EnablePartialRendering="true" runat="server" />
Mark your method as static and give it the WebMethod attribute
The method has to be declared static. It must also be marked with the WebMethod attribute. You'll probably find that you need to include System.Web.Services
using System.Web.Services;
[WebMethod] public static string MyMethod(string name) { return "Hello " + name; }
Call it from Javascript
To call the method from javascript you need to append PageMethods. to the front of the method name.
<script> function test(){ alert(PageMethods.MyMethod("Paul Hayman")); } script>
No comments:
Post a Comment