Question

I have a simple example of page method.but its not getting called.the breakpoint at GetDate() function never get hit and always it shows alert as 'Failure'. aspx page is :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default13.aspx.cs" Inherits="Default13" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
 <script type="text/javascript" language="javascript" >

     function btnTest_onclick() {

         PageMethods.GetDate(Success, Failure);
     }

     function Success(data) {
         alert("success");
         alert(data);
     }

     function Failure(data) {
         alert("failure");
         alert(data.toSource());
     }
    </script>
</head>
<body >
    <form id="form1" runat="server" method="post">
    <div>
     <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" ScriptMode="Debug"  >
    </asp:ScriptManager>
    <%--<input type="button" id="btnSave" value="Save" onclick="btnTest_onclick" />--%>
     <asp:Button id="btnSave" runat="server" Text="Save" OnClientClick="btnTest_onclick()"/>
    </div>
    </form>
</body>
</html>

and the aspx.cs page is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

public partial class Default13 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static string GetDate()
    {
        return "str";
    }
}

any suggestions?

Thanks in Advance,

Was it helpful?

Solution

I think issue is because your button is server side control, will suggest you to call PageMethod from html button instead.

Still If you require to do so,Please try following code.

<asp:Button id="btnSave" runat="server" Text="Save" OnClientClick="btnTest_onclick(); return false;"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top