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

1

7

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;
}
Indistinctive answered 25/1, 2010 at 15:15 Comment(1)
Possible duplicate of How to get controls in static web methodEthyl
E
5

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.

Excepting answered 25/1, 2010 at 15:24 Comment(7)
I'm not sure we understand each other :) I must implement a synchronized list (clientside list must me the same as list on a server) so each change must be posted to server. For example I want to delete some item with ajax - user deletes an item in a list and ajax calls to custom control method which deletes this item in list on server. [WebMethod] should be like a proxy or mediator: JS ajax calls *.aspx method -> this method calls a method on customControlIndistinctive
I wrongly wrote about a web service method but that doesn't change the argument :) . As long as you're using a WebMethod you simply cannot access your controls at that level. You can manage Context object, Session and so on, or call an external library but that's all.<br /> You only have two ways to do what you want:<br /> 1 - Use an UpdatePanel (removing JQuery call). With this solution you can manage your control from the method as you do in a complete postback scenario. 2 - Inject the result of the web method into the existing markup.Excepting
Just one more question - do you know the best way to call methods in CS file from JS through UpdatePanel? How do i use triggers when controlID is in the control itself? Example: User clicks on a link and this link should do a partial postback with some arguments... How do i do that from JS? Short: user do something - js works the magic and then should post (ajax) chaneges (some arguments) to server with updatePanel - how to do that from js? Thanks again...Indistinctive
Once you've placed the controls you need to update asynchronously into one or more UpdatePanels, you don't need JS functions to call directly the methods. You can just assign an EventHandler as you would do normally, and the framework will do the rest. You can find a fairly simple tutorial here: asp.net/learn/ajax/tutorial-01-cs.aspxExcepting
Hmm I kinda understand but I'm not sure this will work. I have a custom control, which has a list of items (with drag&drop jQuery, inline editing and so on). How do I capture these events from UpdatePanel (EventHandlers)? Do I assign triggers with EventNames from events that the customControl has?Indistinctive
No, in fact it won't work ;) Sorry but I didn't understand what you meant by saying "magic". If your control is so strictly tied with jQuery the answer is a bit more complicated. I'll try to answer tomorrow... :)Excepting
Ok:) I really appreciate your help!Indistinctive

© 2022 - 2024 — McMap. All rights reserved.