最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - PAGEMETHODS is not working from JS function - Stack Overflow

programmeradmin4浏览0评论

I am trying to call code behind method from JS function using pagemethods but its not calling and its not throwing any error either...

function example(){

pagemethods.method();
}

**aspx.cs
[webmethod]
public static void method(){

//some logic
}

so to find the issue i did some negative testing for that

  1. I mented WEBMETHOD then it showed an error by saying"object does not support this property or method".can i assume this case shows pagemethods is working!!!

  2. Then I replaced calling method name in JS function to pagemethods.newmethod() but i didn't change method name to newmethod..i was expecting an some error but it didn't give me an error..

NOTE:i have "method=post" in form declaration..does it effect pagemethods anything..

so confused why this issue is happening!!!

can we call codebehind method in any other way instead of pagemethods..please advice!!!

I am trying to call code behind method from JS function using pagemethods but its not calling and its not throwing any error either...

function example(){

pagemethods.method();
}

**aspx.cs
[webmethod]
public static void method(){

//some logic
}

so to find the issue i did some negative testing for that

  1. I mented WEBMETHOD then it showed an error by saying"object does not support this property or method".can i assume this case shows pagemethods is working!!!

  2. Then I replaced calling method name in JS function to pagemethods.newmethod() but i didn't change method name to newmethod..i was expecting an some error but it didn't give me an error..

NOTE:i have "method=post" in form declaration..does it effect pagemethods anything..

so confused why this issue is happening!!!

can we call codebehind method in any other way instead of pagemethods..please advice!!!

Share Improve this question edited Jan 19, 2014 at 16:23 Francisco Corrales Morales 3,8441 gold badge40 silver badges64 bronze badges asked Jan 19, 2014 at 16:20 user3212507user3212507 1732 gold badges3 silver badges11 bronze badges 5
  • why you think that its not calling? – Grundy Commented Jan 19, 2014 at 16:32
  • try see sample Calling Static Methods in an ASP.NET Web Page – Grundy Commented Jan 19, 2014 at 16:50
  • i am debugging code with breakpoints so i dont see that static method got invoked which ever am calling using pagemethods.i am using same concept that got mentioned in "Calling Static Methods in an ASP.NET Web Page" – user3212507 Commented Jan 20, 2014 at 17:17
  • can you provide your markup? – Grundy Commented Jan 20, 2014 at 17:19
  • am able to call methods using pahemethods in sample project but when i use same logic in my actual project it is not working..could u tell me is there any other way i can call those methods from javascript.. – user3212507 Commented Jan 20, 2014 at 17:20
Add a ment  | 

4 Answers 4

Reset to default 3

in msnd you can see a sample of this, so you need...

in markup:

<asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true">
    <Scripts >
        <asp:ScriptReference Path="pm.js" />
    </Scripts>
</asp:ScriptManager>

in code behind: your static method, with attribute [WebMethod]

in pm.js: something like this

function example(){
    PageMethods.method();
}

UPDATE
Another variant is use an ajax request to your method, for example with jquery in goes like this:

function CallMethod(){
    $.ajax({
        type: "POST",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        url: "YourPage.aspx/yourmathod",
        data:JSON.stringify({}), // parameters for method
        success: function (dt) { alert(dt);}, //all Ok
        error: function () { alert('error'); } // some error
    });
}

Perhaps you might be missing EnablePageMethods = true in your aspx page...

This approach is also working -

.aspx file -

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="Microsoft.AspNet.FriendlyUrls" %>
<%@ Import Namespace="Newtonsoft.Json" %>


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

<html xmlns="http://www.w3/1999/xhtml">


<head runat="server">
    <title></title>
    <script type="text/javascript" src="jquery-1.10.2.min.js"></script>

    <script type="text/javascript">

        function CallMethod()
        {
            debugger;


                jQuery.ajax({

                    url: 'Default.aspx/yourmethod',
                    type: "GET",
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    data: JSON.stringify({}), // parameters for method
                    success: function (dt)
                    {
                        debugger;
                        alert('success : ' + dt.d  );

                    }, //all Ok
                    error: function (dt) {
                        debugger;
                        alert('error : ' + dt);
                    } // some error

                });


        }

        function GreetingsFromServer()
        {
            PageMethods.yourmethod1(OnSuccess, OnError);
        }
        function OnSuccess(response)
        {
            debugger;
            alert(response);
        }
        function OnError(error) {
            alert(error);
        }


    </script>


<script language="C#" runat="server">

    [System.Web.Services.WebMethod(EnableSession = true)]  
    [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
    public static string yourmethod()
    {
        var settings = new Microsoft.AspNet.FriendlyUrls.FriendlyUrlSettings();
        settings.AutoRedirectMode = Microsoft.AspNet.FriendlyUrls.RedirectMode.Off;
        string json = Newtonsoft.Json.JsonConvert.SerializeObject("Allow user");
        return json;
    }


     [System.Web.Services.WebMethod(EnableSession = true)]  
    [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
      public static string yourmethod1()
    {
        //string json = Newtonsoft.Json.JsonConvert.SerializeObject("Allow user");
        // or
        return "Allow user";
    }


    </script>
</head>
<body>
    <form id="form1" runat="server" >
        <div>

            <asp:ScriptManager runat="server" EnablePageMethods="true"  EnablePartialRendering="true"  > </asp:ScriptManager>
            <input id="Button1" type="button" value="button" onclick="return GreetingsFromServer();" />
             <input id="Button2" type="button" value="button" onclick="return CallMethod();" />
        </div>
    </form>
</body>
</html>

Web.conf -

<configuration>

  <appSettings>
           <add key="owin:AutomaticAppStartup" value="false" />
  </appSettings>

    <system.web>

            <pilation debug="true" targetFramework="4.5.2" />

            <httpRuntime targetFramework="4.5.2" />

            <authorization>
              <allow users="*"/>
            </authorization>

    </system.web>

</configuration>

Below given code worked for me.

.cs -

using Microsoft.AspNet.FriendlyUrls;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }

    [System.Web.Services.WebMethod(EnableSession = true)] 
    [System.Web.Script.Services.ScriptMethod(UseHttpGet = false, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
    public static string yourmethod1()
    {
         return "Allow user";
    }
}

apsx.cs Page -

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

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

<html xmlns="http://www.w3/1999/xhtml">


<head runat="server">

    <title></title>

     <script type="text/javascript">

        function GreetingsFromServer()
        {
            PageMethods.yourmethod1(OnSuccess, OnError)
        }
        function OnSuccess(response)
        {
            alert(response);
        }
        function OnError(error) 
        {
            alert(error);
        }
    </script>

</head>


<body>

    <form id="form1" runat="server"  method="post" >

    <div>
            <asp:ScriptManager runat="server" EnablePageMethods="true"  EnablePartialRendering="true"  > </asp:ScriptManager>

            <input id="Button1" type="button" value="button" onclick=" return GreetingsFromServer();" />

    </div>

    </form>
</body>
</html>

Web.conf -

<configuration>

  <appSettings>
           <add key="owin:AutomaticAppStartup" value="false" />
  </appSettings>

    <system.web>

            <pilation debug="true" targetFramework="4.5.2" />

            <httpRuntime targetFramework="4.5.2" />

            <authorization>
                       <allow users="*"/>
            </authorization>

    </system.web>

</configuration>
发布评论

评论列表(0)

  1. 暂无评论