c# - 从 javascript 调用 asp.net 页面方法不起作用

标签 c# javascript asp.net pagemethods

您好,我正在从 javascript 调用一个简单的页面方法,这是我在标记处的代码

 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" />

在CS

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

    return "Yes this is working";
 }

警报显示结果并显示“null”。我检查了 Firebug ,但没有从控制台看到错误。

如果我将 TestMethod 更改为

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

和 PageMethod 到

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

它显示正确的响应为“是的,这是有效的”。但是,我需要将参数传递给函数。我想念什么吗?

感谢您的帮助。

最佳答案

我认为主要问题在于您为 ScriptManager 使用的程序集。

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

要解决您在 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>

并在您的 .aspx 页面中使用以下行 -

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

希望这能帮助您解决问题。

关于c# - 从 javascript 调用 asp.net 页面方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10658061/

相关文章:

javascript - 如何在javascript中捕获双键?

javascript - 提交后用成功消息替换 HTML 表单,表单使用单独的 php 文件发送邮件

c# - 从 StackTrace 中过滤掉非用户代码方法

asp.net - LINQ to SQL Web 应用程序最佳实践

asp.net - 从 Web 调用时存储过程较慢,从 Management Studio 调用时较快

c# - 如何向 GridView 添加新行

c# - MicrosoftAppId : '' is unauthorized to post to connector 的安全 token

c# - 我可以将 ANTLR 用于未预处理的代码吗?

javascript - IE9 弹出窗口隐藏在悬停/焦点上

c# - 在 C# 中使用 RotateFlip 旋转图像