tags:

views:

229

answers:

2

This maybe a very noobish question but I am trying to implement a simple web method using AJAX C# 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 it displays the "Something is happening!" message box but does not show the page method alert.

I am using the ASP AJAX toolkit accordion which is why the page load event adds the click handler event to that control.

+2  A: 

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

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

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

You will also need to prevent the SelectedIndexChanging event from doing a Post Back or else the page will not be able to handle the returning result.

tpower
Could you give abit more detail please?
Morgeh
Still no joy. Not sure what the issue is as there are no errors or warnings.
Morgeh
I updated the answer to include code that is working for me.
tpower
+2  A: 

PageMethods on user controls are not supported.

Patrik Potocki
further investigation shows that your quite right and I am just as much of a noob as first thought :) ty
Morgeh