使用JavsScript从客户端调用服务器端的非静态方法

Pra*_*lia 18 c# asp.net asmx webmethod

如何使用javascript(aspx)....从客户端调用服务器端(aspx.cs)中的非静态方法....?

据我所知,我可以从客户端调用服务器端的静态方法...

服务器端:

 [WebMethod]
 public static void method1()
 {
 }
Run Code Online (Sandbox Code Playgroud)

客户端:

 <script language="JavaScript">
     function keyUP() 
     {
         PageMethods.method1();
     }
 </script>
 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
 </asp:ScriptManager>
Run Code Online (Sandbox Code Playgroud)

有用.现在如何从客户端调用非静态方法?

sha*_*tch 17

您可以使用简单的.asmx页面而不是代码隐藏页面来避免静态约束.

1)使用AJAX启用ASP.NET模板打开新网站(它将必要的引用放在web.config中)

2)SIMPLESERVICE.ASMX - 添加一个新的.asmx web服务(我叫雷SimpleService.asmx)注意[System.Web.Script.Services.ScriptSerive]装修的SimpleService类实现web服务.

<%@ WebService Language="C#" Class="SimpleService" %>

using System;
using System.Web.Services;

[System.Web.Script.Services.ScriptService]
public class SimpleService : WebService
{
    [WebMethod]
    public string GetMessage(string name)
    {
        return "Hello <strong>" + name + "</strong>, the time here is: " + DateTime.Now.ToShortTimeString();
    }
} 
Run Code Online (Sandbox Code Playgroud)

3)DEFAULT.ASPX - 要使用它,请在脚本管理器中引用该服务,然后您将关闭并运行.在我的Javascript中,我调用了class.method - SimpleService.GetMessage.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
     <script language="javascript" type="text/javascript">       
        function callServer() {
            SimpleService.GetMessage($get("Name").value, displayMessageCallback);
        }

        function displayMessageCallback(result) {
            $get("message").innerHTML = result;
        } 
    </script>


</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" >
            <Services>
                <asp:ServiceReference Path="~/SimpleService.asmx" />
            </Services>
        </asp:ScriptManager>
        <div>
        </div>
        <h1>Hello World Example</h1>

        <div>
            Enter Name: <input id="Name" type="text" />

            <a href="javascript:callServer()">Call Server</a>

            <div id="message"></div>
        </div>  
    </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我使用了从Scott Gu Found Here中找到的例子 .

  • 请注意,您不应再使用ASMX - 这是一项遗留技术,几乎不受支持.使用WCF可以完成同样的事情. (2认同)

The*_*iot 7

不,你不能从客户端本身调用非静态方法.我曾尝试过一次,但它很难看(我也使用过jQuery ajax).只需使用带有方法名称的ajax调用页面作为查询字符串参数,然后在服务器端检查参数并调用相关方法.但正如我告诉你的那样,它很难看:(

$.ajax({'/mypage.aspx?m=mymethod',......}); //this is not correct syntax
Run Code Online (Sandbox Code Playgroud)

在服务器端:

protected void Page_Load(object sender, EventArgs e)
{
    if(!Request.QueryString.HasKeys() || 
                string.IsNullOrEmpty(Request.QueryString["m"]))
    {
        //return error or something relevant to your code
    }
    var m = Request.QueryString["m"];

    switch(m)
    {
        case "a":
        a();
        break;
        .....
        .....
    }
}
Run Code Online (Sandbox Code Playgroud)