首页 / 脚本样式 / Ajax / ASP.NET AJAX入门系列(3):使用ScriptManagerProxy控件
ASP.NET AJAX入门系列(3):使用ScriptManagerProxy控件2010-10-12TerryLee在ASP.NET AJAX中,由于一个ASPX页面上只能有一个ScriptManager控件,所以在有母版页的情况下,如果需要在Master-Page和Content-Page中需要引入不同的脚本时,这就需要在Content-page中使用ScriptManagerProxy,而不是ScriptManager,ScriptManager 和 ScriptManagerProxy 是两个非常相似的控件。一.ScriptManagerProxy控件概述在ASP.NET AJAX中,由于一个ASPX页面上只能有一个ScriptManager控件,所以在有Master-Page的情况下,如果需要在Master-Page和Content-Page中需要引入不同的脚本时,就需要在Content-page中使用ScriptManagerProxy,而不是ScriptManager,ScriptManagerProxy和 ScriptManager是两个非常相似的控件。简单定义形式如下:<asp:ScriptManagerProxyid="ScriptManagerProxy1"runat="server"> <Services> <asp:ServiceReferencePath="CalculWebService.asmx"/> </Services></asp:ScriptManagerProxy>在它下面可以添加的子标签有:Services,Scripts,AuthenticationService,ProfileService二.简单示例下面看一个简单的使用ScriptManagerProxy的例子。1.首先我们准备两个WebService,在Master-Page中我们输入一个字符串,而在Content-Page中我们求两个数的和。SimpleWebService.asmx[ScriptService] public class SimpleWebService : System.Web.Services.WebService { public SimpleWebService () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public string EchoString(String s) { return "Hello " + s; } } CalculWebService.asmx [ScriptService] public class CalculWebService : System.Web.Services.WebService { public CalculWebService () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public int Add(int a,int b) { return a + b; } }2.添加一个Master-Page,在它上面添加一个ScriptManager控件,并引入WebService SimpleWebService.asmx,并添加相应的HTML元素:<div> <asp:ScriptManagerID="ScriptManager1"runat="server"> <Services> <asp:ServiceReferencePath="SimpleWebService.asmx"/> </Services> </asp:ScriptManager> <asp:contentplaceholderid="ContentPlaceHolder1"runat="server"> </asp:contentplaceholder>