Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

c# - Calling asp.net page method from javascript not working

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...