Home
%3CLINGO-SUB%20id%3D%22lingo-sub-314598%22%20slang%3D%22en-US%22%3EPageMethods%20is%20not%20returning%20a%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-314598%22%20slang%3D%22en-US%22%3E%3CP%3EPageMethods%20is%20a%20JavaScript%20class%20that%20is%20used%20to%20call%20code-behind%20functions%20via%20AJAX%20calls%20in%20the%20background.%26nbsp%3B%20For%20example%3A%20You%20can%20use%20PageMethods%20to%20call%20a%20C%23%20function%20on%20server-side.%20If%20PageMethods%20is%20not%20working%20in%20your%20application%2C%20there%20might%20be%20an%20implementation%20or%20post-back%20issue.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20PageMethods%20to%20work%2C%20there%20should%20be%20a%20JavaScript%20function%20(client-side)%20and%20a%20WebMethod%20function%20(server-side).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESample%20JavaScript%20code%3A%3C%2FP%3E%0A%3CPRE%3E%26lt%3Bscript%20type%3D%22text%2Fjscript%22%26gt%3B%0A%0A%20%20%20function%20callCodeBehind()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20PageMethods.GetMessage(%22test%20string%22%2C%20onSuccess%2C%20onFailure)%3B%0A%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20function%20onSuccess(result%2C%20usercontext%2C%20methodname)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20alert(result)%0A%20%20%20%7D%0A%0A%20%20%20function%20onFailure(error%2C%20usercontext%2C%20methodname)%20%7B%20alert(%22failed%3A%20%22%2B%20error.get_message())%3B%20%7D%0A%0A%26lt%3B%2Fscript%26gt%3B%0A%3C%2FPRE%3E%0A%3CP%3ESample%20C%23%20code%3A%3C%2FP%3E%0A%3CPRE%3Epublic%20partial%20class%20Welcome%20%3A%20System.Web.UI.Page%0A%7B%0A%20%20%20%20%20%20%20%20%20protected%20void%20Page_Load(object%20sender%2C%20EventArgs%20e)%0A%20%20%20%20%20%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%5BWebMethod%5D%0A%20%20%20%20%20%20%20%20%20protected%20static%20void%20GetMessage(string%20val)%0A%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20%22doingit%22%3B%0A%20%20%20%20%20%20%20%20%20%7D%0A%7D%3C%2FPRE%3E%0A%3CP%3EA%20button%20to%20call%20the%20JavaScript%20function%3A%3C%2FP%3E%0A%3CPRE%3E%26lt%3Bbutton%20onclick%3D%22callCodeBehind()%22%26gt%3BClick%20me%26lt%3B%2Fbutton%26gt%3B%20%3C%2FPRE%3E%0A%3CP%3EScriptManager%20(note%20that%20%3CCODE%3EEnablePageMethods%3C%2FCODE%3E%20is%20set%20to%20%3CCODE%3ETrue%3C%2FCODE%3E%3A(%3C%2Fimg%3E%3C%2FP%3E%0A%3CPRE%3E%26lt%3Basp%3AScriptManager%20ID%3D%22ScriptManager1%22%20runat%3D%22server%22%20EnablePageMethods%3D%22True%22%26gt%3B%0A%26lt%3B%2Fasp%3AScriptManager%26gt%3B%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%225%22%3EWhat%20to%20do%20if%20PageMethods%20is%20not%20working%3C%2FFONT%3E%3CBR%20%2F%3EMake%20sure%20the%20implementation%20is%20correct%20as%20per%20the%20guidance%20above.%20However%2C%20even%20though%20you%20implement%20it%20by%20the%20book%2C%20you%20may%20not%20receive%20the%20return%20value.%20In%20my%20case%2C%20the%20issue%20was%20a%20post-back.%20This%20post-back%20was%20happening%20before%20returning%20the%20value.%20Therefore%2C%20the%20page%20was%20losing%20the%20return%20value.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20post-back%20was%20occurring%20because%20of%20my%20friendy%20URL%20setting.%20After%20disabling%20it%2C%20the%20PageMethods%20function%20started%20working.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20need%20to%20disable%20friendly%20URL%20feature%20too%2C%20comment%20out%20the%20line%20below%20in%20your%20%3CCODE%3ERouteConfig%3C%2FCODE%3E%20file.%3C%2FP%3E%0A%3CPRE%3Eroutes.EnableFriendlyUrls(settings)%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20555px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F65980i88EC65FE1CC99DC8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22return.jpg%22%20title%3D%22return.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20left%3B%22%3EThe%20value%20is%20returned%20successfully%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-314598%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20can%20use%20PageMethods%20to%20call%20a%20C%23%20function%20on%20server-side.%20If%20PageMethods%20is%20not%20working%20in%20your%20application%2C%20there%20might%20be%20an%20implementation%20or%20post-back%20issue.%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

PageMethods is a JavaScript class that is used to call code-behind functions via AJAX calls in the background.  For example: You can use PageMethods to call a C# function on server-side. If PageMethods is not working in your application, there might be an implementation or post-back issue.

 

For PageMethods to work, there should be a JavaScript function (client-side) and a WebMethod function (server-side).

 

Sample JavaScript code:

<script type="text/jscript">

   function callCodeBehind() {
                 PageMethods.GetMessage("test string", onSuccess, onFailure);
         }

   function onSuccess(result, usercontext, methodname) {
            alert(result)
   }

   function onFailure(error, usercontext, methodname) { alert("failed: "+ error.get_message()); }

</script>

Sample C# code:

public partial class Welcome : System.Web.UI.Page
{
         protected void Page_Load(object sender, EventArgs e)
         {

         }

         [WebMethod]
         protected static void GetMessage(string val)
         {
                 return "doingit";
         }
}

A button to call the JavaScript function:

<button onclick="callCodeBehind()">Click me</button> 

ScriptManager (note that EnablePageMethods is set to True:(

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>

 

What to do if PageMethods is not working
Make sure the implementation is correct as per the guidance above. However, even though you implement it by the book, you may not receive the return value. In my case, the issue was a post-back. This post-back was happening before returning the value. Therefore, the page was losing the return value.

 

The post-back was occurring because of my friendy URL setting. After disabling it, the PageMethods function started working.

 

If you need to disable friendly URL feature too, comment out the line below in your RouteConfig file.

routes.EnableFriendlyUrls(settings)

return.jpg

The value is returned successfully