问题描述
我有一个 ASP.NET 网站和一个自定义控件(我们称之为 myControl).我需要用 AJAX 在这个控件上调用一个方法.我将 ajax 调用从 JavaScript (jQuery) 发布到 C# WebMethod.这很好用,但我无法在静态 WebMethod 中访问 myControl.任何想法如何解决这个问题?
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?
短版:从 JS 到 C# WebMethod 的 AJAX 调用有效 -> * 此处(在此方法中)我需要在我的自定义控件上调用一个方法,该方法由于静态方法类型而无法访问 *
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 *
[WebMethod]
public static List<CustomListControl.IListItem> GetListItems()
{
// CAN'T GET TO MY CONTROL - need to return myContorl.Items;
return null;
}
推荐答案
嗯,这不是正确的方法.在 Web 服务方法级别,您看不到任何有关页面结构的信息.在这种方法中,您只能加载您的项目列表并将其返回.此列表绑定到的位置与 GetListItems 无关.
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.
您可以通过实现回调函数来管理项目的显示(参见 http://mattberseth.com/blog/2007/06/aspnet_ajax_invoke_a_static_me.html 例如)或使用 UpdatePanel 方法.
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.html for example) or by using the UpdatePanel approach.
这篇关于从静态 [WebMethod] 访问 ASP.NET 控件(JS ajax 调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!