IntroductionASP.NET has very good feature, PageMethod (System.Web.Services.WebMethod). We can call any method which is written in page code behind. We can call it by using jQuery. There are a few constraints while making PageMethod.You can add static page methods to an ASP.NET page and mark them as Web methods. You can then call these methods from script as if they were part of a Web service, but without creating a separate .asmx file. To create Web methods on a page, import the System.Web.Services namespace and add a WebMethodAttribute attribute to each static method that you want to expose. The methods must be marked public more.CodeFirst we have to include the jQuery library.<script type="text/javascript" src="Scripts/jquery-ui-1.8.11.min.js"></script><script type="text/javascript" src="Scripts/jquery-1.6.2.js"></script><script type="text/javascript" src="Scripts/jquery-1.6.2.min.js"></script><script type="text/javascript" src="Scripts/jquery-ui-1.8.11.js"></script>
Create PageMethod in the aspx only:<script runat="server"> [System.Web.Services.WebMethod] public static string GetData() { if (CheckStatus() == 0) return "Hello World"; else return "Hello India"; }</script>Create another static method inside the code behind page:public static int CheckStatus() { return 1;}Now we will make a call from jQuery in Ajax:<script type="text/javascript"> $(document).ready(function () { $.ajax({ type: "POST", url: "Default.aspx/GetData", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { alert(data.d); $("#ctl10_lnkRegister").hide(10);
} }); }); </script>Here is the output screen.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: