Does PageMethods return an undefined result?

I have a very simple call to the PageMethod method. When I look at my PageMethod in my .cs file, the value looks as expected. However, on the client side, I get the result undefined. Any ideas? It should be terribly simple.

Here is my js: ( EnablePageMethods="true" on 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; } } 
+7
c # pagemethods
source share
5 answers

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

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

+1
source share

Here is the answer to calling the PageMethods method using MS Ajax. First, make sure you download 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's all!

+7
source share

You must pass a callback function that will be executed on success / exception. So in this case it will be something like this

 PageMethods.MyMethod("Joe Blow", onSuccess, onError); function onError(desc) { } function onSuccess(result) { } 

I would look at the documentation for exact use.

+3
source share

This is an excellent and specific article on this subject.

The following code works for me.

I have a page that processes an excel file asynchronously; during processing, the EsperarFinDelCargue () function will query the PageMethod method called CargueFinalizo () every second to check if processing has completed. When processing is complete, a redirection occurs.

OnCallFinalizoComplete is a callback function to call PageMethod, so 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 aspx:

 [System.Web.Services.WebMethod] public static string CargueFinalizo() { //Whatever you need return HttpContext.Current.Session["ResultadoCarguePanel"] != null ? "SI" : "NO"; } 
0
source share
 Try This it will work fine <script type="text/javascript"> function Generate() { var result = PageMethods.GenerateOTP(your parameter, function (response) { alert(response); }); } </script> 
0
source share

All Articles