且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

.net Ajax系列(2)调用多Web Service

更新时间:2022-01-11 08:56:36

 

scriptManager的多服务调用和ScriptManagerProxy调用WebService

1)  母版页

添加母版页,并向页添加ScriptManager,为它添加2ws引用(多服务引用)

<asp:ScriptManager ID="ScriptManager1" runat="server">

        <Services>

            <asp:ServiceReference Path="selfWS.asmx" />

            <asp:ServiceReference Path="ThirdWs.asmx" />

        </Services>

</asp:ScriptManager>

 

两个WS如下:

[ScriptService]

public class selfWS : System.Web.Services.WebService {

[WebMethod]

public string ShowName(string s)

{

        return "This's your name:" + s;

}

}

//=======================================================

[ScriptService]

public class ThirdWs : System.Web.Services.WebService {

    [WebMethod]

    public string HelloWorld() {

        return "the third ws showing";

    }

}

2)   母版页添加对两个WS的方法调用。

<input type="button" value="MasterClick" onclick="showName()" />

<input type="button" value="ThirdClick" onclick="showThird()" />

 

<script>

        function showName()

        {

            selfWS.ShowName("master",showww);

        }

        function showww(result)

        {

            alert(result);

        }

       

        function showThird()

        {

            ThirdWs.HelloWorld(

            function success(result)

            {

                alert(result);

            }

            );

        }

</script>

3)   新加子页,启用这个母版

点击2个按钮,测试完成。

4)   在这个子页中添加ScriptManagerProxy,同时为其添加对另一个WS的引用

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">

    <Services>

        <asp:ServiceReference Path="SelfWSs.asmx" />

    </Services>

</asp:ScriptManagerProxy>

WS的内容如下:

[ScriptService]

public class SelfWSs : System.Web.Services.WebService {

    [WebMethod]

    public string ShowNamess(string str1,string str2)

    {

        return "this is no Master Name:"+str1+str2;

    }

}

5)   预览完成。

备注:此示例有以下几个点:

母版页(或ScriptManager)对多个WS的引用并调用它们的方法;

对已经启动带有ScriptManager的母版页的子页来说,如果调用其它WS的方法;

调用WS的参数问题。

博客园大道至简

http://www.cnblogs.com/jams742003/

转载请注明:博客园