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

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

问题描述

我如何使用JavaScript(Java)调用从客户端服务器端(aspx.cs)非静态方法....?

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...

服务器端:

 [WebMethod]
 public static void method1()
 {
 }

客户端:

 <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?

推荐答案

您可以通过使用简单的.asmx页,而不是codebehind页避免静电约束。

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

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

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

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

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 - 要使用它引用的服务于你的脚本经理,你是关闭和运行。在我的javascript我称之为class.method - SimpleService.GetMessage

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>

我用我从斯科特谷发现的例子
<一href=\"http://weblogs.asp.net/scottgu/archive/2006/10/22/Tip_2F00_Trick_3A00_-Cool-UI-Templating-Technique-to-use-with-ASP.NET-AJAX-for-non_2D00_UpdatePanel-scenarios.aspx\">Found这里

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆