调用一个asp.net AJAX服务器控件从客户端的公共职能 [英] calling a public function of an asp.net ajax server control from client side
问题描述
我想在ASP.NET和在应用程序,我有一个文本框,一个AJAX服务器控件,我想发送文本框的文字功能在ASP.NET AJAX服务器控件类创建和函数返回一些结果是基于文本。
我的应用程序使用它从外部DLL引入服务器控件添加作为参考。该服务器控件将使用AJAX来完成它的功能。
要使用我的控制,我想补充的脚本管理器和我的控制在.aspx页面上,它应该开始工作。
- 添加脚本管理器页面
- 在添加新的Web服务,文件添加到项目
- 属性[ScriptService]添加到服务类
- 创建一个方法,接受并返回一个字符串,即:
- 属性[ScriptMethod]添加到方法
- 在与脚本管理的aspx页面,添加一个服务引用ASMX文件
- 调用服务器端方法在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的(命名空间=http://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.
- Add a Script Manager to the page
- Add a new web service file to the project
- Add the attribute [ScriptService] to the service class
- Create a method that accepts and returns a string ie:
- Add the attribute [ScriptMethod] to the method
- On the aspx page with the script manager, add a Service reference to the asmx file
- 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屋!