使用 JavsScript 从客户端调用服务器端的非静态方法 [英] Call non-static method in server-side from client-side using JavsScript

查看:34
本文介绍了使用 JavsScript 从客户端调用服务器端的非静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

服务器端:

 [WebMethod]公共静态无效方法1(){}

客户端:

 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>

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

解决方案

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

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

2) SIMPLESERVICE.ASMX - 添加一个新的 .asmx 网络服务(我称之为 SimpleService.asmx)注意 [System.Web.Script.Services.ScriptServive] 修饰和 SimpleService 类实现 Webservice.

<%@ WebService Language="C#" Class="SimpleService" %>使用系统;使用 System.Web.Services;[System.Web.Script.Services.ScriptService]公共类 SimpleService : WebService{[网络方法]公共字符串 GetMessage(字符串名称){return "你好 <strong>"+ name + "</strong>,这里的时间是:" + DateTime.Now.ToShortTimeString();}}

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="服务器"><title>无标题页</title><script language="javascript" type="text/javascript">函数调用服务器(){SimpleService.GetMessage($get("Name").value, displayMessageCallback);}功能 displayMessageCallback(结果){$get("message").innerHTML = 结果;}<身体><form id="form1" runat="server"><asp:ScriptManager ID="ScriptManager1" runat="server" ><服务><asp:ServiceReference Path="~/SimpleService.asmx"/></服务></asp:ScriptManager><div>

<h1>Hello World 示例</h1><div>输入名称:<input id="Name" type="text"/><a href="javascript:callServer()">呼叫服务器</a><div id="消息"></div>

</表单>

我使用了从 Scott Gu 找到的示例在这里找到.

How do I call a non-static method in server side(aspx.cs) from client side using javascript (aspx)....?

As far as I know I can call static method in server side from client side...

server side:

 [WebMethod]
 public static void method1()
 {
 }

client side:

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

It works. Now how do I call non-static method from client side?

解决方案

You can avoid the static constraint by using a simple .asmx page instead of the codebehind page.

1) Open New Website using the AJAX Enable ASP.NET template (it puts the necessary references in the web.config)

2) SIMPLESERVICE.ASMX - Add a new .asmx web service (I called mine SimpleService.asmx) Notice the [System.Web.Script.Services.ScriptSerive] decoration and that the SimpleService class implements Webservice.

<%@ 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();
    }
} 

3) DEFAULT.ASPX - To use it reference the service in you script manager and you are off and running. In my Javascript I call the 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>

I used the example I found from Scott Gu Found Here.

这篇关于使用 JavsScript 从客户端调用服务器端的非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆