C # AJAX Web Methods - What Am I Doing Wrong?

This may be a very Nubian question, but I am trying to implement a simple web method using C # AJAX and asp.net here is the code:

C # code behind:

using System.Web.Services;

public partial class Controls_LeftNavigation : System.Web.UI.UserControl
{
    [WebMethod]
    public static string MyMethod()
    {
        return "Hello";
    }
}

      

Asp.net page:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>

<script type="text/javascript">
            function pageLoad() {
                var acc = $find('<%= Accordion1.ClientID %>_AccordionExtender');
                acc.add_selectedIndexChanging(ClickHandler);
            }
            function ClickHandler() {
                // Do whatever you want.
                alert('Something is happening!');
                alert(PageMethods.MyMethod());
            }
</script>

      

when the navigation button is clicked, "Something is happening!" is displayed, but no page method warning is displayed.

I'm using an ASP AJAX toolkit accordion so the page load event adds a click handler event to this element.

+2


source to share


2 answers


PageMethods is not supported on custom controls.



+2


source


The page method is asynchronous, you must provide an onSuccess handler like this:

function OnSuccess(result) {
   alert(result);
}

function ClickHandler() {
   PageMethods.MyMethod(OnSuccess);
}

      



You also need to prevent the SelectedIndexChanging

back message from generating an event , otherwise the page will not be able to process the returned result.

+2


source







All Articles