Calling asp.net page method from javascript not working

windforceus picture windforceus · May 18, 2012 · Viewed 23.4k times · Source

Hi I am calling a simple page method from javascript , here is my code at markup

 function OnCallSumComplete(result, userContext, methodName) {             
            alert(result);
 }
 function OnCallSumError(error, userContext, methodName) {
     if (error !== null) {
         alert(error.get_message());
     }
 }
 function test(){
     var contextArray = "";
     PageMethods.TestMethod("test parameter", OnCallSumComplete, OnCallSumError,  contextArray);
 }

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

at cs

 [System.Web.Services.WebMethod]
 public static string TestMethod(string para)
 {

    return "Yes this is working";
 }

the alert show the result and it says "null". I check firebug and i don't see error from console.

If i change the TestMethod to

 [System.Web.Services.WebMethod]
 public static string TestMethod()
 {
    return "Yes this is working";
 }

And PageMethod to

 PageMethods.TestMethod( function (response) { alert(response);  } );

It shows the correct response as "Yes this is working". However, i need to pass parameter to the function. Do i miss anything?

Thanks for help.

Answer

Shiv Mohan Saxena picture Shiv Mohan Saxena · Sep 16, 2012

I think the main problem is with the assembly you are using for ScriptManager.

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

To resolve your problem use in Webconfig -

<pages>
  <controls>
    <add tagPrefix="ajax" 
         namespace="System.Web.UI" 
         assembly="System.Web.Extensions, 
                   Version=1.0.61025.0, 
                   Culture=neutral, 
                   PublicKeyToken=31bf3856ad364e35"/>
  </controls>
</pages>

and in your .aspx page use following lines -

<ajax:ScriptManager ID="ScriptManager1" 
                    EnablePageMethods="true" 
                    runat="server" />

Hope this will help you to resolve your problem.