PageMethods returning undefined result?

Clay picture Clay · Aug 24, 2009 · Viewed 12.7k times · Source

I have a very simple call to a PageMethod. When I step through my PageMethod in my .cs file, the value looks as expected. However, on the client side I get an undefined result. Any ideas? This should be horribly simple.

Here is my js: (EnablePageMethods="true" in my ASPX page)

function test() {
    alert(PageMethods.MyMethod("Joe Blow"));
}

And here is my C#:

public partial class test : System.Web.UI.Page 
{
    [WebMethod]
    public static string MyMethod(string name)
    {
        return "Hello " + name;
    }
}

Answer

azamsharp picture azamsharp · Aug 25, 2009

Here is the answer on how to call PageMethods using MS Ajax. First make sure you have downloaded the latest Ajax library from the MS website.

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

    <input type="button" value="Greeting" onclick="greetings()" />

<script language="javascript" type="text/javascript">

    function greetings() {

       PageMethods.GreetingFromPage(function(response) {

            alert(response);

         });

    }


</script>

   [WebMethod]
        public static string GreetingFromPage()
        {
            return "greeting from page"; 
        }

That is pretty much it!