C# 从静态 [WebMethod] 访问 ASP.NET 控件(JS ajax 调用)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2133194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:45:38  来源:igfitidea点击:

Access ASP.NET control from static [WebMethod] (JS ajax call)

c#asp.netajaxuser-controlspagemethods

提问by Heko

I have a ASP.NET WebSite and a custom control (lets call it myControl) on it. I need to call a method on this control with AJAX. I'm posting ajax call from JavaScript (jQuery) to C# WebMethod. This works fine, but I can't get to myControl in a static WebMethod. Any ideas how to solve this problem?

我有一个 ASP.NET 网站和一个自定义控件(我们称之为 myControl)。我需要使用 AJAX 在这个控件上调用一个方法。我正在发布从 JavaScript (jQuery) 到 C# WebMethod 的 ajax 调用。这工作正常,但我无法在静态 WebMethod 中访问 myControl。任何想法如何解决这个问题?

Short version: AJAX call from JS to C# WebMethod works -> * here (in this method) I need to call a method on my custom control which is inaccessible because of static method type *

简短版本:从 JS 到 C# WebMethod 的 AJAX 调用有效 -> * 此处(在此方法中)我需要在我的自定义控件上调用一个方法,该方法由于静态方法类型而无法访问 *

[WebMethod]
public static List<CustomListControl.IListItem> GetListItems()
{
    // CAN'T GET TO MY CONTROL - need to return myContorl.Items;
    return null;
}

采纳答案by mamoo

Well, that's not the correct approach. At the web service method level you cannot see anything about the page structure. In this method you can only load your list of items and return it. Where this list is binded to is none of GetListItems' business.

嗯,这不是正确的方法。在 Web 服务方法级别,您看不到有关页面结构的任何信息。在这种方法中,您只能加载您的项目列表并返回它。此列表绑定到的位置与 GetListItems 无关。

You can manage the display of the Items by implementing a callback function (see http://mattberseth.com/blog/2007/06/aspnet_ajax_invoke_a_static_me.htmlfor example) or by using the UpdatePanel approach.

您可以通过实现回调函数(例如,参见http://mattberseth.com/blog/2007/06/aspnet_ajax_invoke_a_static_me.html)或使用 UpdatePanel 方法来管理项目的显示。