7

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;
    }
}
skaffman
  • 398,947
  • 96
  • 818
  • 769
Clay
  • 1,273
  • 2
  • 16
  • 23
  • I take it the EnablePageMethods attribute is on a ScriptManager control in your page? – Russ Cam Aug 24 '09 at 19:26
  • No, to your second question, I read that earlier...haven't found an answer to this here so far. Thanks. – Clay Aug 24 '09 at 19:32
  • Hmmm... this is very strange. I have Page Methods numerous times and have not seen this behaviour before - I set up a demo on my local machine (VS2008 3.5 SP1) and to my surprise PageMethods did not appear to be working correctly for me either. Using firebug (or fiddler) I can see a HTTP Post is made but it calls the onFailure function when it does (interestingly, with no error message). If I call the PageMethod from the console in firebug, again I see the HTTP post and the response this time is the expected one. What version and pack of Visual Studio are you using? – Russ Cam Aug 24 '09 at 21:47
  • To clarify, when the PageMethod is called from inside the page, the HTTP Post returns 200 ok and the response is expected, but what happens on the page is that the failure function gets called and the result is "The Server method [xxx] failed". A page refresh then occurs straight after. As I have said, I have used PageMethods numerous times and not seen this behaviour before. – Russ Cam Aug 24 '09 at 22:15
  • Basically the same experience I had. I'm running Visual Studio 2008 SP1. The only difference now is that I'm running Windows 7 Enterprise (instead of Vista Ultimate as I was before). Making the call via JSON is working for me so that's what I'm sticking with for now. – Clay Aug 26 '09 at 12:14

5 Answers5

7

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!

azamsharp
  • 19,710
  • 36
  • 144
  • 222
3

You've to pass in a callback function that would be executed on Success / Exception. So in this case, it would be something like this

PageMethods.MyMethod("Joe Blow", onSuccess, onError);

function onError(desc) {
}

function onSuccess(result) {
}

I would check the documentation for the exact usage.

Vasu Balakrishnan
  • 1,751
  • 10
  • 15
1

Check out the following screencast. It explains how to call the PageMethods using JQuery:

http://www.highoncoding.com/Articles/430_Calling_Page_Methods_Using_JQuery_and_Returning_JSON_Result.aspx

azamsharp
  • 19,710
  • 36
  • 144
  • 222
0
Try This it will work fine

     <script type="text/javascript">
       function Generate()
       {              
           var result = PageMethods.GenerateOTP(your parameter, function (response)
           {
               alert(response);
           });
       }
</script>
Ankit
  • 4,755
  • 2
  • 22
  • 14
0

This is a great and concrete article on the subject.

For me, the following code is working.

I have a page that processes an excel file asynchronously; while processing, the function EsperarFinDelCargue() polls a PageMethod called CargueFinalizo() each second to see if processing has ended. When processing finishes, a redirection takes place.

OnCallFinalizoComplete is the callback function for the PageMethod invocation, so there is where you need to use the resulting object.

<script type="text/javascript">       

    function EsperarFinDelCargue()
    {           
        PageMethods.CargueFinalizo(OnCallFinalizoComplete);            
        if($('#<%=this.hidCargueFinalizado.ClientID %>').val() == "SI")
        {
            document.location = "CargarPanelHCP.aspx";
        }
        else
        {
            var t=setTimeout("EsperarFinDelCargue()",1000);                
        }
    }

    function OnCallFinalizoComplete(result,contexto,CargueFinalizo)
    {               
        $('#<%=this.hidCargueFinalizado.ClientID %>').val(result);            
    }
</script>

And here is the PageMethod code in the aspx:

[System.Web.Services.WebMethod]
public static string CargueFinalizo()
{
    //Whatever you need
    return HttpContext.Current.Session["ResultadoCarguePanel"] != null ? "SI" : "NO";
}
daniloquio
  • 3,822
  • 2
  • 36
  • 56