2017-04-19 63 views
0

我正在使用Visual Studio 2015并且项目在.NET 4.5.2中 我选择了默认模板,所以有一些东西像Master Site和Default.aspx解决方案,但我没有碰他们...如何在ASP.net中设置PageMethods

我添加了一个页面,Welcome.aspx和似乎无法让PageMethods正常工作。

Welcome.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Welcome.aspx.cs" Inherits="MyProject.Welcome" %> 
<!DOCTYPE html> 
<html> 
    <head> 
     <title></title> 
     <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" /> 
     <script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script> 
     <link rel="stylesheet" href="CSS/WelcomeCSS.css" /> 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> 
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script> 
     <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css" /> 
    </head> 

    <body> 
     <form id="WelcomeForm" runat="server"> 
     <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server"></asp:ScriptManager> 

      <div> ... some more html... non <asp:controls>...</div> 

      <script type="text/javascript"> 

      //PageMethods.TestMarker(); // throws exception - PageMethods is undefined. 

      $.ajax({ 
       type: "POST", 
       dataType: 'text', 
       contentType: "text", 
       url: "Welcome.aspx/TestMarker()", 
       data: "{val = adrian}", // parameters for method 
       success: function (dt) { alert("HI"+dt); }, //all Ok 
       error: function (dt) { alert(dt); } // some error 
      }); // doesn't seem to throw an error but C# never gets called 
      </script> 
     </form> 
    </body> 
    </html> 

Welcome.aspx.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

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

     } 

     [WebMethod] 
     protected static void TestMarker(string val) 
     { 
      return; 
     } 

    } 
} 

我已经有以下引用: - System.Web.Extensions程序

问:我错过了打电话给我的服务器端代码吗?

+0

方法应该是公开的,而不是保护。 – Boney

+0

更改您的TestMarker静态方法公共而不是受保护的 – UJS

+0

更容易使用PageMethods方式吗? – fifamaniac04

回答

0

受保护的WebMethod转变为公共如下:

[WebMethod] 
     Public static void TestMarker(string val) 
     { 
      return; 
     } 

更改您的客户端脚本调用Ajax如下:

<script>  
$.ajax({ 
        type: "POST", 
        dataType: 'text', 
        contentType: "text", 
        url: "Welcome.aspx/TestMarker", 
        data: '{ val:"adrian" }', // parameters for method 
        success: function (data) { alert("HI" + data.d); }, //all Ok 
        error: function (data) { alert(data); } // some error 
       }); 
</script> 

说明: - 请记住以下几点:

public

类型或成员可以被同一程序集或引用它的其他程序集中的任何其他代码访问。

保护

的类型或成员只能由代码在相同的类或结构来访问,或在派生类。

私人

的类型或成员只能由代码在相同的类或结构进行访问。

+0

试过这个,仍然无法打到我的C# – fifamaniac04

+0

fifamaniac04,我只是更新代码并相应地更新你的代码...你的客户端调用有一些错误 – UJS

+0

在VS中,修改后,我在输出窗口中得到以下内容.....抛出异常:'System.Threading.ThreadAbortException'in mscorlib.dll 抛出异常:'System。ThreadingAbortException'在System.Web.dll中 – fifamaniac04

0

尝试进行更改以下行的代码:

public static void TestMarker(string val) //Method should be public 
url: "Welcome.aspx/TestMarker", //Dont need enclosing braces 
dataType: "json", 
contentType: "application/json; charset=utf-8", 
data: JSON.stringify({val:"adrian"}), 
+0

试过这个,但仍然无法打到我的C# – fifamaniac04

+0

你会得到什么错误? – Boney

+0

mscorlib.dll中的'System.Threading.ThreadAbortException' – fifamaniac04