【问题标题】:how to use jQuery ajax with asp.net user controls?如何将 jQuery ajax 与 asp.net 用户控件一起使用?
【发布时间】:2013-02-26 11:37:29
【问题描述】:

我想用 ajax 和 asp.net 用户控件,

    $.ajax({
        type: "POST",
        url: "*TCSection.ascx/InsertTCSection",
        data: "{id:'" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var URL = msg.d;
            alert(URL);
        });

.cs代码

[WebMethod]
public static string InsertTCSection(string id)
{
    string result = "deneme";
    return result; 
}

【问题讨论】:

  • 您遇到错误了吗?
  • 我在服务器端函数添加断点,它没有被命中
  • 你不能通过jquery调用保存在UserControl中的方法,因为在运行时没有.ascx这样的资源,它与保存它的页面合并

标签: asp.net ajax jquery


【解决方案1】:

您不能通过 jquery 调用保存在用户控件中的方法。 因为 .ascx 控件不代表真实的 url。

它们旨在嵌入到某些 ASP.NET 页面中,因此它们在运行时不存在。 您可能会做的是,创建一个单独的服务并将您的方法放在那里。 喜欢webservices.

请参阅thisthis 和许多其他人

【讨论】:

    【解决方案2】:

    我正在使用通用处理程序来解决这个问题。

    【讨论】:

      【解决方案3】:

      试试:

       $.ajax({
              type: "POST",
              url: "*TCSection.ascx/InsertTCSection",
              data: JSON2.stringify({ id: id}, //Your 2nd id is passing value but i dont know where its come from
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function (msg) {
                  var URL = msg.d;
                  alert(URL);
                   }
               )};
      

      在cs中:

      [WebMethod]
      public static string InsertTCSection(string id)
      {
          string result="deneme";
          return result; 
      }
      

      【讨论】:

      • id 参数不重要我想了解 .ascx 的 ajax。有什么办法吗?
      【解决方案4】:

      我认为您可能缺少此属性

         [System.Web.Script.Services.ScriptService]
      

      在你服务类之前

      [System.Web.Script.Services.ScriptService]
      public class TCSection: System.Web.Services.WebService
      {
      
          [WebMethod]
          public static string InsertTCSection(string id)
          {
          }
       }
      

      或者可能还有其他原因导致 web 服务的路径不正确。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        • 2012-03-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多