从客户端调用asp.net ajax服务器控件的公共函数 [英] calling a public function of an asp.net ajax server control from client side

查看:23
本文介绍了从客户端调用asp.net ajax服务器控件的公共函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to make a ajax server control in ASP.NET and in that application I have a textbox and I want to send text of that textbox to function that is created in ASP.NET ajax server control class and that function return some result based on text.

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

My Application uses Server controls which are Imported from External DLL added as a reference. This Server Control will make use of AJAX to complete its functionality.

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

To use My control, I would add the Script Manager and My Control on the .aspx page and it should start working.

推荐答案

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

MyPage.aspx:

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

MyService.asmx

MyService.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;
        }
    }
}

这篇关于从客户端调用asp.net ajax服务器控件的公共函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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