scriptmanager用法_scriptmanager控件

scriptmanager用法_scriptmanager控件ScriptManager控件包括在ASP.NET2.0AJAXExtensions中,它用来处理页面上的所有组件以及页面局部更新,生成相关的客户端代理脚本以便能够在JavaScript中访问WebService,所有

ScriptManager
控件包括在ASP.NET 2.0 AJAX Extensions中,它用来处理页面上的所有组件以及页面局部更新,生成相关的客户端代理脚本以便能够在JavaScript中访问Web Service,所有需要支持ASP.NET AJAX的ASP.NET页面上有且只能有一个ScriptManager控件。在ScriptManager控件中我们可以指定需要的脚本库,或者指定通过JS来调用的Web Service,以及调用AuthenticationService和ProfileService,还有页面错误处理等。

 
主要内容
1
.控件概述
2
.一个简单的示例
3
.客户端脚本模式
4
.错误处理
5
.Services属性
6
.Scripts属性
 
一.控件概述
ScriptManager
控件包括在ASP.NET 2.0 AJAX Extensions中,它用来处理页面上的所有组件以及页面局部更新,生成相关的客户端代理脚本以便能够在JavaScript中访问Web Service,所有需要支持ASP.NET AJAX的ASP.NET页面上有且只能有一个ScriptManager控件。在ScriptManager控件中我们可以指定需要的脚本库,或者指定通过JS来调用的Web Service,还可以指定页面错误处理等。
使用<asp:ScriptManager/>来定义一个ScriptManager,简单的ScriptManager定义形式:
 

scriptmanager用法_scriptmanager控件
<
asp:ScriptManager 
ID
=”ScriptManager1″
 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                   runat

=”server”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件      

<
AuthenticationService 
Path
=”” 
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件      

<
ProfileService 
LoadProperties
=””
 Path
=”” 
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件      

<
Scripts
>


scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        
<asp:ScriptReference/>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件      

</
Scripts
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件      

<
Services
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        

<
asp:ServiceReference 
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件      

</
Services
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
asp:ScriptManager
>

ScriptManager
属性和方法如下:

 
属性/方法
描述
AllowCustomError
和Web.config中的自定义错误配置区<customErrors>相联系,是否使用它,默认值为true
AsyncPostBackErrorMessage
异步回传发生错误时的自定义提示错误信息,
AsyncPostBackTimeout
异步回传时超时限制,默认值为90,单位为秒
EnablePartialRendering
是否支持页面的局部更新,默认值为True,一般不需要修改
ScriptMode
指定ScriptManager发送到客户端的脚本的模式,有四种模式:Auto,Inherit,Debug,Release,默认值为Auto,后面会仔细说到。
ScriptPath
设置所有的脚本块的根目录,作为全局属性,包括自定义的脚本块或者引用第三方的脚本块。如果在Scripts中的<asp:ScriptReference/>标签中设置了Path属性,它将覆盖该属性。
OnAsyncPostBackError
异步回传发生异常时的服务端处理函数,在这里可以捕获一场信息并作相应的处理。
OnResolveScriptReference
指定ResolveScriptReference事件的服务器端处理函数,在该函数中可以修改某一条脚本的相关信息如路径、版本等。
 
二.一个简单的示例
这个例子其实是UpdatePanel示例,在页面中加入了日期控件和一个下拉框,根据下拉框选择的不同,日期控件背景变为不同的颜色。示例代码如下:
<%@ Page Language=C# AutoEventWireup=true CodeFile=Default.aspx.cs Inherits=_Default %>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件
<script runat=”server”>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    
void DropDownSelection_Change(Object sender, EventArgs e)
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件    
{

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        Calendar1.DayStyle.BackColor 
=
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件            System.Drawing.Color.FromName(ColorList.SelectedItem.Value);
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    }

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件
</script>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件
<html xmlns=”http://www.w3.org/1999/xhtml”>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件
<head id=”Head1″ runat=”server”>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    
<title>ScriptManager Example</title>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件
</head>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件
<body>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    
<form id=”form1″ runat=”server”>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        
<div>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件            
<asp:ScriptManager ID=”ScriptManager1″ 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                               runat
=”server”>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件            
</asp:ScriptManager>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件            
<asp:UpdatePanel ID=”UpdatePanel1″
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                             runat
=”server”>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                
<ContentTemplate>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                    
<asp:Calendar ID=”Calendar1″ 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                                  ShowTitle
=”True”
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                                  runat
=”server” />
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                    
<div>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                        Background:
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                        
<br />
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                        
<asp:DropDownList ID=”ColorList” 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                                          AutoPostBack
=”True” 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                                          OnSelectedIndexChanged
=”DropDownSelection_Change”
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                                          runat
=”server”>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                            
<asp:ListItem Selected=”True” Value=”White”> 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                            White 
</asp:ListItem>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                            
<asp:ListItem Value=”Silver”> 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                            Silver 
</asp:ListItem>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                            
<asp:ListItem Value=”DarkGray”> 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                            Dark Gray 
</asp:ListItem>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                            
<asp:ListItem Value=”Khaki”> 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                            Khaki 
</asp:ListItem>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                            
<asp:ListItem Value=”DarkKhaki”> D
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                            ark Khaki 
</asp:ListItem>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                        
</asp:DropDownList>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                    
</div>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                
</ContentTemplate>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件            
</asp:UpdatePanel>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件            
<br />
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        
</div>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    
</form>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件
</body>
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件
</html>

 
三.客户端脚本模式
在前面我们提到了ScriptMode属性指定ScriptManager发送到客户端的脚本的模式,它有四种模式:Auto,Inherit,Debug,Release,默认值为Auto。
1
.Auto:它会根据Web站点的Web.config配置文件来决定使用哪一种模式,只有当配置文件中retail属性设置为false:
.Inherit:应该是通过程序设置ScriptMode的时候,等同于Auto?(不太了解)
scriptmanager用法_scriptmanager控件
<
system
.web
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件  

<
deployment 
retail
=”false” 
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
system.web
>
或者页面中的Debug指令设为true的时候会使用Debug版本,其他的情况都会使用Release版本。
scriptmanager用法_scriptmanager控件
<
%@ Page 
Language
=”C#”
 Debug
=”true”

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件AutoEventWireup

=”true”
 CodeFile
=”Default.aspx.cs”
 Inherits
=”_Default”
 %
>
2
3
.Debug:客户端脚本使用Debug版本,除非retail属性设为true。
4
.Release:客户端脚本使用Release版本,除非retail属性设为false。
 
四.错误处理
在页面回传时如果发生了异常AsyncPostBackError事件将被触发,错误信息的处理依赖于AllowCustomErrors属性、AsyncPostBackErrorMessage属性和Web.config中的<customErrors>配置区。下面看一个简单的错误处理例子,在AsyncPostBackError事件中捕获到异常信息并设置AsyncPostBackErrorMessage属性。
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件
<%

@ Page Language=C# 
%>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件

<
script 
runat
=”server”
>


scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    protected 
void ErrorProcessClick_Handler(object sender, EventArgs e)
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件    
{

scriptmanager用法_scriptmanager控件        
// This handler demonstrates an error condition. In this example
scriptmanager用法_scriptmanager控件

scriptmanager用法_scriptmanager控件        
// the server error gets intercepted on the client and an alert is shown. 
scriptmanager用法_scriptmanager控件

scriptmanager用法_scriptmanager控件        
throw new ArgumentException();
scriptmanager用法_scriptmanager控件    }

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    protected 
void SuccessProcessClick_Handler(object sender, EventArgs e)
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件    
{

scriptmanager用法_scriptmanager控件        
// This handler demonstrates no server side exception.
scriptmanager用法_scriptmanager控件

scriptmanager用法_scriptmanager控件        UpdatePanelMessage.Text 
= The asynchronous postback completed successfully.;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    }

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    protected 
void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件    
{

scriptmanager用法_scriptmanager控件        ScriptManager1.AsyncPostBackErrorMessage 
= 异常信息为: + e.Exception.Message;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    }

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
script
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

<
html 
xmlns
=”http://www.w3.org/1999/xhtml”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

<
head 
id
=”Head1″
 runat
=”server”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

<
title
>
PageRequestManager endRequestEventArgs Example
</
title
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件    

<
style 
type
=”text/css”
>


scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件    body 
{

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        font-family
: Tahoma;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    
}

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件    #AlertDiv
{

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    left
: 40%; top: 40%;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    position
: absolute; width: 200px;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    padding
: 12px; 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    border
: #000000 1px solid;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    background-color
: white; 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    text-align
: left;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    visibility
: hidden;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    z-index
: 99;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    
}

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件    #AlertButtons
{

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    position
: absolute;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    right
: 5%;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    bottom
: 5%;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    
}

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

</
style
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
head
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

<
body 
id
=”bodytag”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

<
form 
id
=”form1″
 runat
=”server”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        

<
div
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件            

<
asp:ScriptManager 
ID
=”ScriptManager1″
 runat
=”server”
 
scriptmanager用法_scriptmanager控件            OnAsyncPostBackError

=”ScriptManager1_AsyncPostBackError”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件            

</
asp:ScriptManager
>

scriptmanager用法_scriptmanager控件 
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件            

<
script 
type
=”text/javascript”
 language
=”javascript”
>


scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                
var divElem = ‘AlertDiv’;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                
var messageElem = ‘AlertMessage’;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                
var errorMessageAdditional = ‘Please try again.’;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                
var bodyTag = ‘bodytag’;
scriptmanager用法_scriptmanager控件                Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                
function ToggleAlertDiv(visString)
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件                
{

scriptmanager用法_scriptmanager控件                     
if (visString == ‘hidden’)
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件                     
{

scriptmanager用法_scriptmanager控件                         $get(bodyTag).style.backgroundColor 
= ‘white’;                         
scriptmanager用法_scriptmanager控件                     }

scriptmanager用法_scriptmanager控件                     
else
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件                     
{

scriptmanager用法_scriptmanager控件                         $get(bodyTag).style.backgroundColor 
= ‘gray’;                         
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                     }

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                     
var adiv = $get(divElem);
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                     adiv.style.visibility 
= visString;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                }

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件                
function ClearErrorState() {

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                     $get(messageElem).innerHTML 
= ”;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                     ToggleAlertDiv(‘hidden’);                     
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                }

scriptmanager用法_scriptmanager控件                
function EndRequestHandler(sender, args)
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件                
{

scriptmanager用法_scriptmanager控件                   
if (args.get_error() != undefined && args.get_error().httpStatusCode == ‘500‘)
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件                   
{

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                       
var errorMessage = args.get_error().message
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                       args.set_errorHandled(
true);
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                       ToggleAlertDiv(‘visible’);
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                       $get(messageElem).innerHTML 
= ‘‘ + 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                                errorMessage + ‘
 ‘ + errorMessageAdditional;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                   }

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                }

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件            

</
script
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件            

<
asp:UpdatePanel 
runat
=”Server”
 UpdateMode
=”Conditional”
 ID
=”UpdatePanel1″
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                

<
ContentTemplate
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                    

<
asp:Panel 
ID
=”Panel1″
 runat
=”server”
 GroupingText
=”Update Panel”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                        

<
asp:Label 
ID
=”UpdatePanelMessage”
 runat
=”server” 
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                        

<
br 
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                        Last update:
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                        

<%
=
 DateTime.Now.ToString() 
%>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                        .
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                        

<
br 
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                        

<
asp:Button 
runat
=”server”
 ID
=”Button1″
 Text
=”Submit Successful Async Postback”

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                            OnClick

=”SuccessProcessClick_Handler”
 OnClientClick
=”ClearErrorState()” 
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                        

<
asp:Button 
runat
=”server”
 ID
=”Button2″
 Text
=”Submit Async Postback With Error”

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                            OnClick

=”ErrorProcessClick_Handler”
 OnClientClick
=”ClearErrorState()” 
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                        

<
br 
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                    

</
asp:Panel
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                

</
ContentTemplate
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件            

</
asp:UpdatePanel
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件            

<
div 
id
=”AlertDiv”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                

<
div 
id
=”AlertMessage”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                

</
div
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                

<
br 
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                

<
div 
id
=”AlertButtons” 
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                    

<
input 
id
=”OKButton”
 type
=”button”
 value
=”OK”
 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                           runat

=”server”
 onclick
=”ClearErrorState()” 
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件                

</
div
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件           

</
div
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

</
form
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
body
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
html
>
运行后时界面:
scriptmanager用法_scriptmanager控件
发生异常信息:
scriptmanager用法_scriptmanager控件
 
五.Services属性
Services
用来管理对WebService的调用,通过<asp:ServiceReference>标签可以在Services中注册一个WebService,在运行时ScriptManager将为每一个ServiceReference对象生成一个客户端代理,<asp:ServiceReference>标签一个很重要的属性是Path,用来指定WebService的路径,如下所示:
scriptmanager用法_scriptmanager控件
<
asp:ScriptManager 
ID
=”SM1″
 runat
=”server”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

<
Services
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        

<
asp:ServiceReference 
Path
=”Service.asmx”
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

</
Services
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
asp:ScriptManager
>
看一个简单的调用WebService的例子:
WebService
如下,注意在WebServiceSample上加ScriptService特性:
页面:
scriptmanager用法_scriptmanager控件
[ScriptService]
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件

public 
class
 WebServiceSample : System.Web.Services.WebService 

{

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    
public WebServiceSample()
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件    
{

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        
//Uncomment the following line if using designed components 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        
//InitializeComponent(); 
scriptmanager用法_scriptmanager控件

scriptmanager用法_scriptmanager控件    }

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    [WebMethod]
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    
public string EchoString(String s)
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件    
{

scriptmanager用法_scriptmanager控件        
return Hello  + s;
scriptmanager用法_scriptmanager控件    }

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件}
ASPX
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件
<%

@ Page Language=C# AutoEventWireup=true CodeFile=Default3.aspx.cs Inherits=Default3 
%>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

<
html 
xmlns
=”http://www.w3.org/1999/xhtml” 
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

<
head 
runat
=”server”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

<
title
>
Untitled Page
</
title
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
head
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件

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


scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    
function OnbuttonGo_click() 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件    
{

scriptmanager用法_scriptmanager控件        requestSimpleService 
= WebServiceSample.EchoString(
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件            document.getElementById(‘inputName’).value,       
//params
scriptmanager用法_scriptmanager控件

scriptmanager用法_scriptmanager控件            OnRequestComplete    
//Complete event
scriptmanager用法_scriptmanager控件

scriptmanager用法_scriptmanager控件            );
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        
return false;
scriptmanager用法_scriptmanager控件    }

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    
function OnRequestComplete(result) 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件    
{

scriptmanager用法_scriptmanager控件        alert(result);
scriptmanager用法_scriptmanager控件    }

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
script
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

<
body
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

<
form 
id
=”form1″
 runat
=”server”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

<
asp:ScriptManager 
ID
=”ScriptManager1″
 runat
=”server”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        

<
Services
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件            

<
asp:ServiceReference 
Path
=”WebServiceSample.asmx”
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        

</
Services
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

</
asp:ScriptManager
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

<
div
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        

<
input 
type
=”text”
 id
=”inputName”
 size
=20
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        

<
input 
id
=”button”
 type
=”button”
 value
=”调 用”
 onclick
=”return OnbuttonGo_click()” 
/></
div
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

</
form
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
body
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
html
>
运行后效果如下:
scriptmanager用法_scriptmanager控件
当然了也可以在运行时动态的在Services中加入ServiceReference,下面看一个运行时动态加入ServiceReference的例子:
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件
<%

@ Page Language=C# AutoEventWireup=true CodeFile=Default3.aspx.cs Inherits=Default3 
%>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件

<
script 
runat
=”server”
>


scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    
void Page_Load(object sender, EventArgs e)
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件    
{

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        ServiceReference sr 
= new ServiceReference();
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        sr.Path 
= WebServiceSample.asmx;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        ScriptManager1.Services.Add(sr);
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    }

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
script
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

<
html 
xmlns
=”http://www.w3.org/1999/xhtml” 
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

<
head 
runat
=”server”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

<
title
>
Untitled Page
</
title
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
head
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件

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


scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    
function OnbuttonGo_click() 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件    
{

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        requestSimpleService 
= WebServiceSample.EchoString(
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件            document.getElementById(‘inputName’).value,       
//params
scriptmanager用法_scriptmanager控件

scriptmanager用法_scriptmanager控件            OnRequestComplete    
//Complete event
scriptmanager用法_scriptmanager控件

scriptmanager用法_scriptmanager控件            );
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        
return false;
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    }

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    
function OnRequestComplete(result) 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件scriptmanager用法_scriptmanager控件    
{

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        alert(result);
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    }

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
script
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件 
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

<
body
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    
scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

<
form 
id
=”form1″
 runat
=”server”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

<
asp:ScriptManager 
ID
=”ScriptManager1″
 runat
=”server”
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

</
asp:ScriptManager
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

<
div
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        

<
input 
type
=”text”
 id
=”inputName”
 size
=20
/>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件        

<
input 
id
=”button”
 type
=”button”
 value
=”调 用”
 onclick
=”return OnbuttonGo_click()” 
/></
div
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件    

</
form
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
body
>

scriptmanager用法_scriptmanager控件
scriptmanager用法_scriptmanager控件

</
html
>
可以看到运行后和在ScriptManager中直接加入的效果是一样的。
scriptmanager用法_scriptmanager控件
 
六.Scripts属性
关于Scripts属性到后面具体再说吧,最主要的属性有Path指定脚本的路径,ScriptMode指定客户端脚本的模式,它会覆盖ScriptManager中的ScriptMode属性,还有一个属性是IgnoreScriptPath,指定是否忽略掉ScriptManager中的ScriptPath属性。

 今天的文章scriptmanager用法_scriptmanager控件分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/72517.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注