views:

1146

answers:

1

Hello!

Guys, I need some help. 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;
}
+1  A: 

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.

mamoo
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 customControl
Heko
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.
mamoo
Thank you! I will try ;)
Heko
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...
Heko
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: http://www.asp.net/learn/ajax/tutorial-01-cs.aspx
mamoo
Heko
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... :)
mamoo
Ok:) I really appreciate your help!
Heko