【问题标题】:WebMethod access page control and set valueWebMethod 访问页面控制和设置值
【发布时间】:2015-12-05 12:54:33
【问题描述】:

我的页面名称

public partial class AtamaGorevDegistir : System.Web.UI.Page
{}

我的 webmethod ajax 端

var path = getLocation(location.href);
$.ajax({
    type: "POST",
    url: path.pathname + "/KisiBilgiDoldur",
    //  data: "{" + str + "}",
    contentType: "application/json; charset=utf-8",
    //  dataType: "json",
    success: function (data) {
        var dd = data.d;
        $('.modal-dialog').css({ width: '85%' });
        $('#AtamaModal').modal({ show: true });
    }
});

我的网络方法

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string KisiBilgiDoldur(string KayitID, string TeklifID)
{
   AtamaGorevDegistir atama = new AtamaGorevDegistir();
   atama.AtananSuren.Value = "123";
   return null;
}

但我的问题是我的 Control 是 null 。但我可以访问此方法但没有设置值并给出错误消息。为什么会这样?

【问题讨论】:

  • 发送参数和数据。您的数据是评论。

标签: asp.net ajax webmethod


【解决方案1】:

WebMethod 和 ScriptMethod 无法访问调用页面的控件集合。将其视为正常的 Web 表单生命周期之外。当你使用 AJAX 时,你需要将所有数据传递给它需要的服务器端方法,而你的服务器端应该返回客户端需要的所有数据。然后客户端应该获取返回的数据并根据需要操作 DOM 以显示结果。

在您的 WebMethod 中,返回数据而不是尝试将其分配给控件,删除 return null;

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string KisiBilgiDoldur(string KayitID, string TeklifID)
{
   return "123";
}

在您的客户端成功处理程序中,获取返回的数据并使用 JavaScript 设置控件的值。

var path = getLocation(location.href);
$.ajax({
    type: "POST",
    url: path.pathname + "/KisiBilgiDoldur",
    //  data: "{" + str + "}",
    contentType: "application/json; charset=utf-8",
    //  dataType: "json",
    success: function (data) {
        var dd = data.d;
        $('.modal-dialog').css({ width: '85%' });
        $('#AtamaModal').modal({ show: true });
        //set control value to data.d here
    }
});

【讨论】:

  • 我知道这种方法并正在使用,但我的问题与此无关。我想访问控制,因为我使用混合组件并且我没有设置客户端。我需要服务器端设置。
  • @smtprld 但这不是它的工作原理。说你希望它以这种方式工作并不会改变它的工作方式。
  • 这种方式对我不起作用。因为我不想这样做,因为返回对我来说太晚了。我将使用 therad 和更快的工作来处理大数据。如果你是专业人士,你就知道。我可以使用 ashx 等。你知道如何以 webmethod 或其他方式访问页面控制。我不问你如何工作 ajax 。请问如何访问页面控制和设置。如果你知道,不知道。不要说我怎么工作。
  • @smtp 我是“专业人士”。我付钱来写代码,而且我已经做了一段时间了。如果您确实了解 AJAX、Web 窗体控件生命周期以及 WebMethod 的工作原理,您就会明白您所要求的实际上是不可能的,并且我已经为您提供了一个可行的解决方法。如果你选择不听,那很好。但是,如果您不想听答案,为什么要在这里提问呢?不管你喜不喜欢,它都不会改变网络运作的现实。
  • 你是传奇。你什么都知道。听听你的回答。你不想了解我。但它不是我的答案理解。
【解决方案2】:

您不使用 web 方法设置控件值。您从 api 返回一个值并在浏览器中对其进行处理。使用 webmethods 不像 webforms。您在 webmethod 中返回的值是 null ......您在 js 中将该 null 值设置为“dd”,但您不使用它做任何事情。当您从 webmethod 中获取您的价值时,您需要对它做一些事情。在您的成功回调中尝试“alert(data.d)”以了解我的意思。然后 $('#elm').text(data.d) 如果你想要它在页面上。

【讨论】:

  • 我知道使用 return data.d 但我正在使用 devexpress 控制,我需要服务器端来做它
  • 获取 devexpress 控件的 id 并使用 devexpress 客户端功能用 webmethod 返回的值填充它
  • 问题不是。这么简单的问题如何在Webmethod中访问页面控件?
  • 我认为您无法按照自己的方式进行操作。请参阅@mason 的回答。
【解决方案3】:

你应该给数据添加参数。

$.ajax({
        type: "POST",
        url: path.pathname + "/KisiBilgiDoldur",
        data:JSON.stringify({ KayitID: '1', TeklifID:'2' }), 
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            var dd = data.d;

            $('.modal-dialog').css({ width: '85%' });
            $('#AtamaModal').modal({ show: true });
        }
    });

【讨论】:

  • 兄弟,请阅读我的网络方法,我正在发送参数,但不是我的问题,我想访问控制和设置值。
  • @smtprld 我们这里没有“兄弟”的人。不是每个人都不是男性,也不是每个人都喜欢被称为男性。您传递参数的行已在您的问题中注释掉。
猜你喜欢
  • 2015-01-16
  • 1970-01-01
  • 2016-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多