Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

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

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?

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...