Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

calling a public function of an asp.net ajax server control from client side

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  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;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...