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

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

问题描述

我想在ASP.NET和在应用程序,我有一个文本框,一个AJAX服务器控件,我想发送文本框的文字功能在ASP.NET AJAX服务器控件类创建和函数返回一些结果是基于文本。

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

要使用我的控制,我想补充的脚本管理器和我的控制在.aspx页面上,它应该开始工作。

解决方案
  1. 添加脚本管理器页面
  2. 在添加新的Web服务,文件添加到项目
  3. 属性[ScriptService]添加到服务类
  4. 创建一个方法,接受并返回一个字符串,即:
  5. 属性[ScriptMethod]添加到方法
  6. 在与脚本管理的aspx页面,添加一个服务引用ASMX文件
  7. 调用服务器端方法在JavaScript中使用完整的命名空间限定它。

MyPage.aspx:

  ...
< ASP:ScriptManager的ID =ScriptManager1=服务器>
    <服务>
        < ASP:服务引用路径=〜/ MyService.asmx/>
    < /服务>
< / ASP:ScriptManager的>
...
<脚本>
    MyNameSpace.MyService.MyMethod(一些文本,responseHandlerMethod,errorHandlerMethod);
< / SCRIPT>
...
 

MyService.asmx

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Services;
使用System.Web.Script.Services;

命名空间了myNameSpace
{
    [WebService的(命名空间=htt​​p://tempuri.org/)]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
    [System.ComponentModel.ToolboxItem(假)]
    [ScriptService]
    公共类MyServiceClass:System.Web.Services.WebService
    {
        [ScriptMethod]
        [WebMethod的]
        公共字符串的MyMethod(字符串SomeText)
        {
            返回妈妈你好!+ SomeText;
        }
    }
}
 

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.

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.

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

解决方案

  1. Add a Script Manager to the page
  2. Add a new web service file to the project
  3. Add the attribute [ScriptService] to the service class
  4. Create a method that accepts and returns a string ie:
  5. Add the attribute [ScriptMethod] to the method
  6. On the aspx page with the script manager, add a Service reference to the asmx file
  7. Call the server side method in javascript qualifying it with the full namespace.

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

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天全站免登陆