1

我想在 ASP.NET 中创建一个 ajax 服务器控件,在该应用程序中我有一个文本框,我想将该文本框的文本发送到在 ASP.NET ajax 服务器控件类中创建的函数,并且该函数返回一些基于文本。

我的应用程序使用从作为参考添加的外部 DLL 导入的服务器控件。此服务器控件将利用 AJAX 来完成其功能。

要使用我的控件,我将在 .aspx 页面上添加脚本管理器和我的控件,它应该会开始工作。

4

1 回答 1

1
  1. 向页面添加脚本管理器
  2. 将新的 Web 服务文件添加到项目中
  3. 将属性 [ScriptService] 添加到服务类
  4. 创建一个接受并返回字符串的方法,即:
  5. 将属性 [ScriptMethod] 添加到方法中
  6. 在带有脚本管理器的 aspx 页面上,添加对 asmx 文件的服务引用
  7. 在 javascript 中调用服务器端方法,使用完整的命名空间对其进行限定。

我的页面.aspx:

...
<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
        <asp:ServiceReference Path="~/MyService.asmx" />
    </Services>
</asp:ScriptManager>
...
<script>
    MyNameSpace.MyService.MyMethod('some text', responseHandlerMethod, errorHandlerMethod);
</script>
...

我的服务.asmx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

namespace MyNameSpace
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    public class MyServiceClass: System.Web.Services.WebService
    {
        [ScriptMethod]
        [WebMethod]
        public string MyMethod(string SomeText)
        {
            return "Hi mom! " + SomeText;
        }
    }
}
于 2011-06-15T11:44:56.407 回答