7

我对 PageMethod 有一个非常简单的调用。当我在 .cs 文件中单步执行 PageMethod 时,该值看起来符合预期。但是,在客户端我得到一个未定义的结果。有任何想法吗?这应该非常简单。

这是我的 js:(EnablePageMethods="true"在我的 ASPX 页面中)

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

这是我的 C#:

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

5 回答 5

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!

于 2009-08-25T18:15:00.953 回答
3

您必须传入一个将在 Success/Exception 上执行的回调函数。所以在这种情况下,它会是这样的

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

function onError(desc) {
}

function onSuccess(result) {
}

我会检查文档以了解确切的用法。

于 2009-08-24T19:53:34.863 回答
1

查看以下截屏视频。它解释了如何使用 JQuery 调用 PageMethods:

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

于 2009-08-24T19:37:57.643 回答
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";
}
于 2012-03-27T20:18:52.823 回答
0
Try This it will work fine

     <script type="text/javascript">
       function Generate()
       {              
           var result = PageMethods.GenerateOTP(your parameter, function (response)
           {
               alert(response);
           });
       }
</script>
于 2013-12-20T06:12:55.167 回答