ASP.NET AJAX入门系列(3):使用ScriptManagerProxy控件

ASP.NET AJAX中,由于一个ASPX页面上只能有一个ScriptManager控件,所以在有母版页的情况下,如果需要在Master-PageContent-Page中需要引入不同的脚本时,这就需要在Content-page中使用ScriptManagerProxy,而不是ScriptManagerScriptManager ScriptManagerProxy 是两个非常相似的控件。

主要内容

1ScriptManagerProxy控件概述

2.简单示例

 

一.ScriptManagerProxy控件概述

ASP.NET AJAX中,由于一个ASPX页面上只能有一个ScriptManager控件,所以在有Master-Page的情况下,如果需要在Master-PageContent-Page中需要引入不同的脚本时,就需要在Content-page中使用ScriptManagerProxy,而不是ScriptManagerScriptManagerProxyScriptManager是两个非常相似的控件。简单定义形式如下:

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

    
<Services>

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

     
</Services>

</asp:ScriptManagerProxy>
在它下面可以添加的子标签有:ServicesScriptsAuthenticationServiceProfileService

二.简单示例

下面看一个简单的使用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:ScriptManager ID="ScriptManager1" runat="server" >

        
<Services>

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

        
</Services>

    
</asp:ScriptManager>

    
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">

    
</asp:contentplaceholder>

    
&nbsp;<h3>请输入名称:</h3>

    
<input id="inputName" type="text" />

    
<input id="button" type="button" value="确 定" onclick="return OnbuttonGo_click()" />

</div>

编写相应的JS代码:

<script type="text/javascript" language="JavaScript">

    
function OnbuttonGo_click() 

    
{
        requestSimpleService 
= SimpleWebService.EchoString(

            document.getElementById('inputName').value,       
//params

            OnRequestComplete    
//Complete event

            );

        
return false;
    }


    
function OnRequestComplete(result) 

    
{
        alert(result);
    }


</script>

3.添加一个Content-Page,在它上面添加一个ScriptManagerProxy控件,并引入WebService CalculWebService.asmx,并添加相应的HTML元素:

<div>

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

        
<Services>

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

         
</Services>

    
</asp:ScriptManagerProxy>

    
<h3>请输入两个数:</h3>&nbsp;<input id="inputA" type="text" style="width: 110px" />&nbsp;+&nbsp;

    
<input id="inputB" style="width: 110px" type="text" />&nbsp;

    
<input id="buttonEqual" type="button" value=" = "  onclick="return OnbuttonEqual_click()"/>

</div>

编写相应的JS代码:

<script type="text/javascript" language="JavaScript">

    
function OnbuttonEqual_click() 
    
{
        requestSimpleService 
= CalculWebService.Add(

            document.getElementById('inputA').value,       
//params

            document.getElementById('inputB').value,       
//params

            OnRequestComplete    
//Complete event

            );

        
return false;
    }


    
function OnRequestComplete(result) 

    
{
        alert(result);
    }


</script>
4
.运行后界面如下:

测试Master-Page中的Web Service

测试Content-Page中的Web Service

关于ScriptManagerProxy就介绍到这儿,有个问题就是在我的IDE中为什么ScriptManagerProxy总是提示为未知元素,但运行起来并不抱错,结果也可以出来?
示例代码下载:/Files/Terrylee/ASPNETAJAXScriptManagerProxyDemo.rar

作者:TerryLee
出处:http://terrylee.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted @ 2006-10-27 08:31 TerryLee 阅读(21063) 评论(101)  编辑 收藏 网摘 所属分类: [08]  Web开发[07]  AJAX风云

  回复  引用  查看    
#1楼 2006-10-27 09:17 | 阿一      
嘿,我的不会提示未知元素咯。
  回复  引用    
#2楼 2006-10-27 09:31 | 小庄[匿名] [未注册用户]
已阅,thank you
  回复  引用    
#3楼 2006-10-27 09:34 | anikin [未注册用户]
我也没提示未知元素
  回复  引用    
#4楼 2006-10-27 09:37 | Ben[匿名] [未注册用户]
已查阅.呵呵

想问一下,现在关于AJAX的书籍有那些比较好的.
  回复  引用  查看    
#5楼 2006-10-27 10:31 | nestle[匿名]      
好,每天支持一下
  回复  引用  查看    
#6楼 2006-10-27 10:52 | jailu      
Good good study,Day day up
  回复  引用  查看    
#7楼 2006-10-27 11:01 | vagabond      
支持一下..学习!!
  回复  引用  查看    
#8楼 2006-10-27 11:03 | vagabond      
@Terrylee
这篇文章没结合源码吗?
  回复  引用    
#9楼 2006-10-27 11:19 | Raysbo [未注册用户]
支持。。。
一直在看你的系列^_^

  回复  引用  查看    
#10楼 2006-10-27 13:02 | 舒密      
你好,我在母版页放了ScriptManager控件,在其它页面的ContentPlaceHolder中放一下UpdatePanel总是出现下面的错误

The control with ID 'UpdatePanel1' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.

说没有找到ScriptManager 请问怎么解决??
  回复  引用  查看    
#11楼 2006-10-27 14:01 | aspnetx      
喜欢TerryLee的文章
短小精练
一看就懂
  回复  引用  查看    
#12楼 2006-10-27 14:05 | aspnetx      
还有
如果有些地方有必要的话,简单的说一下与之前的atlas的区别吧
  回复  引用  查看    
#13楼 2006-10-27 16:17 | 木野狐[匿名]      
有没有试过如何在 Content Page 和 Master Page 的 html 元素之间做协同调用?
  回复  引用    
#14楼 2006-10-27 16:47 | TheOLD(远古) [未注册用户]
拜读 加油
  回复  引用  查看    
#15楼 [楼主]2006-10-27 17:49 | TerryLee      
@阿一
不知道我的为什么总是提示呢-_-
  回复  引用  查看    
#16楼 [楼主]2006-10-27 17:50 | TerryLee      
@小庄[匿名]
不用客气,多交流:)
  回复  引用  查看    
#17楼 [楼主]2006-10-27 17:51 | TerryLee      
@anikin
看来不是ASP.NET AJAX的问题了,是我个人的问题,唉……
  回复  引用  查看    
#18楼 [楼主]2006-10-27 17:56 | TerryLee      
@Ben[匿名]
Atlas之前倒是有基本,改为ASP.NET AJAX之后好像还没有什么书籍,AJAX我不太了解:)
  回复  引用  查看    
#19楼 [楼主]2006-10-27 17:58 | TerryLee      
@nestle[匿名]
欢迎……
  回复  引用  查看    
#20楼 [楼主]2006-10-27 17:58 | TerryLee      
@jailu
@vagabond
:)
  回复  引用  查看    
#21楼 [楼主]2006-10-27 17:59 | TerryLee      
@vagabond
有源码,我晚上回去放上来吧,忘了发了
  回复  引用  查看    
#22楼 [楼主]2006-10-27 18:00 | TerryLee      
@Raysbo
谢谢哦:)
  回复  引用  查看    
#23楼 [楼主]2006-10-27 18:01 | TerryLee      
@舒密
我回去看一下吧,UpdatePanel还没仔细研究呢
  回复  引用  查看    
#24楼 [楼主]2006-10-27 18:02 | TerryLee      
@aspnetx
谢谢你的建议,后面的文章我会注意,关于变化部分Dflying Chen的拥抱变化系列不错,可以看看……
  回复  引用  查看    
#25楼 [楼主]2006-10-27 18:03 | TerryLee      
@木野狐[匿名]
没做过测试,呵呵
  回复  引用  查看    
#26楼 [楼主]2006-10-27 18:03 | TerryLee      
@TheOLD(远古)
谢谢:)
  回复  引用    
#27楼 2006-10-27 23:54 | kiwi [未注册用户]
@vagabond
有源码,我晚上回去放上来吧,忘了发了
期待源碼
  回复  引用    
#28楼 2006-10-28 01:08 | Hoodlum [未注册用户]
好东西.谢谢....
  回复  引用    
#29楼 2006-10-28 16:05 | minghui227 [未注册用户]
我碰到个问题好郁闷,希望大家帮我解决下
我在button的onclick事件中,调用js中的方法它怎么老说我并不包含OnbuttonGo_click()的定义,我太郁闷了?
这是怎么回事?我代码是复制过来测试过的。
谁来拯救下我!!!!!
  回复  引用  查看    
#30楼 [楼主]2006-10-28 16:21 | TerryLee      
@Hoodlum
客气:)

@minghui227
把你ASPX页面中的代码贴上来看看那儿有疏忽啊?这样也不知道什么原因
  回复  引用  查看    
#31楼 2006-10-28 20:51 | seyon      
@TerryLee
我有个问题问下,以前应用过atlas的项目,要迁移到 ASP.NET AJAX,webconfig里面需要做哪些更改?
  回复  引用    
#32楼 2006-10-29 13:45 | lauralxj [未注册用户]
@舒密
我今天也碰到了这个问题,然后解决了,希望能共同学习。
是这样的:
如果主版页面有二个或者更多的ContentPlaceHolder 你把
ScriptManager放到第一个ContentPlaceHolder上面当然要在
<form runat="server">里面 形状如下:
<form runat="server" id="myform">

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

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
</asp:ContentPlaceHolder>




  回复  引用  查看    
#33楼 [楼主]2006-10-29 16:59 | TerryLee      
Atlas中的Web.config:
<?xml version="1.0"?>

<configuration>

    
<configSections>
        
<sectionGroup name="microsoft.web" type="Microsoft.Web.Configuration.MicrosoftWebSectionGroup">
            
<section name="converters" type="Microsoft.Web.Configuration.ConvertersSection" requirePermission="false"/>
            
<section name="webServices" type="Microsoft.Web.Configuration.WebServicesSection" requirePermission="false"/>
            
<section name="authenticationService" type="Microsoft.Web.Configuration.AuthenticationServiceSection" requirePermission="false"/>
            
<section name="profileService" type="Microsoft.Web.Configuration.ProfileServiceSection" requirePermission="false"/>
        
</sectionGroup>
    
</configSections>

    
<microsoft.web>
        
<converters>
            
<add type="Microsoft.Web.Script.Serialization.Converters.DataSetConverter"/>
            
<add type="Microsoft.Web.Script.Serialization.Converters.DataRowConverter"/>
            
<add type="Microsoft.Web.Script.Serialization.Converters.DataTableConverter"/>
        
</converters>
        
<webServices enableBrowserAccess="true"/>

    
</microsoft.web>
    
<appSettings/>
    
<connectionStrings/>
    
<system.web>
        
<pages>
            
<controls>
                
<add namespace="Microsoft.Web.UI" assembly="Microsoft.Web.Atlas" tagPrefix="atlas"/>
                
<add namespace="Microsoft.Web.UI.Controls" assembly="Microsoft.Web.Atlas" tagPrefix="atlas"/>
            
</controls>
        
</pages>

        
<compilation debug="true">
            
<buildProviders>
                
<add extension=".asbx" type="Microsoft.Web.Services.BridgeBuildProvider"/>
            
</buildProviders>
        
</compilation>

        
<httpHandlers>
            
<remove verb="*" path="*.asmx"/>
            
<add verb="*" path="*.asmx" type="Microsoft.Web.Services.ScriptHandlerFactory" validate="false"/>

            
<add verb="*" path="atlasbatchcall.axd" type="Microsoft.Web.Services.MultiRequestHandler" validate="false"/>
            
<add verb="*" path="atlasglob.axd" type="Microsoft.Web.Globalization.GlobalizationHandler" validate="false"/>

            
<add verb="*" path="*.asbx" type="Microsoft.Web.Services.ScriptHandlerFactory" validate="false"/>
        
</httpHandlers>
        
<httpModules>
            
<add name="ScriptModule" type="Microsoft.Web.Services.ScriptModule"/>
            
<add name="BridgeModule" type="Microsoft.Web.Services.BridgeModule"/>
            
<add name="WebResourceCompression" type="Microsoft.Web.Services.WebResourceCompressionModule"/>
        
</httpModules>

    
</system.web>

</configuration>
ASP.NET AJAX中的Web.config:
<?xml version="1.0"?>
<configuration>
    
<configSections>
        
<sectionGroup name="microsoft.web" type="Microsoft.Web.Configuration.MicrosoftWebSectionGroup, Microsoft.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
            
<sectionGroup name="scripting" type="Microsoft.Web.Configuration.ScriptingSectionGroup, Microsoft.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
                
<