简体   繁体   中英

How to setup PageMethods in ASP.net

I'm using Visual Studio 2015 and the project is in .NET 4.5.2 I choose the default template so there are some things like the Master Site and Default.aspx that are in the solution but I have not touched them...

I added a page, Welcome.aspx and Can't seem to get the PageMethods to work properly.

Welcome.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Welcome.aspx.cs" Inherits="MyProject.Welcome" %>
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
        <script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
        <link rel="stylesheet" href="CSS/WelcomeCSS.css" />
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script>
        <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css" />
    </head>

    <body>
         <form id="WelcomeForm" runat="server">
         <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server"></asp:ScriptManager>

          <div> ... some more html... non <asp:controls>...</div>

          <script type="text/javascript">

             //PageMethods.TestMarker(); // throws exception - PageMethods is undefined.

             $.ajax({
                type: "POST",
                dataType: 'text',
                contentType: "text",
                url: "Welcome.aspx/TestMarker()",
                data: "{val = adrian}", // parameters for method
                success: function (dt) { alert("HI"+dt); }, //all Ok
                error: function (dt) { alert(dt); } // some error
            }); // doesn't seem to throw an error but C# never gets called
          </script>
       </form>
     </body>
    </html>

Welcome.aspx.cs

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

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

        }

        [WebMethod]
        protected static void TestMarker(string val)
        {
            return;
        }

    }
}

I have the following references: - system.web.extensions

Q: What am I missing to call my server side code?

Change your Protected WebMethod to Public as follows :

[WebMethod]
        Public static void TestMarker(string val)
        {
            return;
        }

Change your client side script ajax call as follows :

<script>    
$.ajax({
                    type: "POST",
                    dataType: 'text',
                    contentType: "text",
                    url: "Welcome.aspx/TestMarker",
                    data: '{ val:"adrian" }', // parameters for method
                    success: function (data) { alert("HI" + data.d); }, //all Ok
                    error: function (data) { alert(data); } // some error
                }); 
</script>

Explanation :- Do remember following points :

public

The type or member can be accessed by any other code in the same assembly or another assembly that references it.

protected

The type or member can only be accessed by code in the same class or struct, or in a derived class.

private

The type or member can only be accessed by code in the same class or struct.

Try making changes to the below lines of code:

public static void TestMarker(string val) //Method should be public
url: "Welcome.aspx/TestMarker", //Dont need enclosing braces
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({val:"adrian"}),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM