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定义形式:
属性和方法如下:
|
属性/方法
|
描述
|
|
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示例,在页面中加入了日期控件和一个下拉框,根据下拉框选择的不同,日期控件背景变为不同的颜色。示例代码如下:

<%
@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>



<scriptrunat="server">

voidDropDownSelection_Change(Objectsender,EventArgse)


{
Calendar1.DayStyle.BackColor=
System.Drawing.Color.FromName(ColorList.SelectedItem.Value);
}
</script>


<htmlxmlns="http://www.w3.org/1999/xhtml">
<headid="Head1"runat="server">
<title>ScriptManagerExample</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:ScriptManagerID="ScriptManager1"
runat="server">
</asp:ScriptManager>
<asp:UpdatePanelID="UpdatePanel1"
runat="server">
<ContentTemplate>
<asp:CalendarID="Calendar1"
ShowTitle="True"
runat="server"/>
<div>
Background:
<br/>
<asp:DropDownListID="ColorList"
AutoPostBack="True"
OnSelectedIndexChanged="DropDownSelection_Change"
runat="server">
<asp:ListItemSelected="True"Value="White">
White</asp:ListItem>
<asp:ListItemValue="Silver">
Silver</asp:ListItem>
<asp:ListItemValue="DarkGray">
DarkGray</asp:ListItem>
<asp:ListItemValue="Khaki">
Khaki</asp:ListItem>
<asp:ListItemValue="DarkKhaki">D
arkKhaki</asp:ListItem>
</asp:DropDownList>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<br/>
</div>
</form>
</body>
</html>
三.客户端脚本模式
在前面我们提到了ScriptMode属性指定ScriptManager发送到客户端的脚本的模式,它有四种模式:Auto,Inherit,Debug,Release,默认值为Auto。
1.Auto:它会根据Web站点的Web.config配置文件来决定使用哪一种模式,只有当配置文件中retail属性设置为false:.Inherit:应该是通过程序设置ScriptMode的时候,等同于Auto?(不太了解)
<system.web>
<deploymentretail="false"/>
</system.web>
或者页面中的Debug指令设为true的时候会使用Debug版本,其他的情况都会使用Release版本。
<%@PageLanguage="C#"Debug="true"
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属性。

<%
@PageLanguage="C#"%>

<scriptrunat="server">

protectedvoidErrorProcessClick_Handler(objectsender,EventArgse)


{
//Thishandlerdemonstratesanerrorcondition.Inthisexample
//theservererrorgetsinterceptedontheclientandanalertisshown.
thrownewArgumentException();
}
protectedvoidSuccessProcessClick_Handler(objectsender,EventArgse)


{
//Thishandlerdemonstratesnoserversideexception.
UpdatePanelMessage.Text="Theasynchronouspostbackcompletedsuccessfully.";
}
protectedvoidScriptManager1_AsyncPostBackError(objectsender,AsyncPostBackErrorEventArgse)


{
ScriptManager1.AsyncPostBackErrorMessage="异常信息为:"+e.Exception.Message;
}
</script>

<htmlxmlns="http://www.w3.org/1999/xhtml">
<headid="Head1"runat="server">
<title>PageRequestManagerendRequestEventArgsExample</title>

<styletype="text/css">


body{
}{
font-family:Tahoma;
}

#AlertDiv{
}{
left:40%;top:40%;
position:absolute;width:200px;
padding:12px;
border:#0000001pxsolid;
background-color:white;
text-align:left;
visibility:hidden;
z-index:99;
}

#AlertButtons{
}{
position:absolute;
right:5%;
bottom:5%;
}
</style>
</head>
<bodyid="bodytag">
<formid="form1"runat="server">
<div>
<asp:ScriptManagerID="ScriptManager1"runat="server"
OnAsyncPostBackError="ScriptManager1_AsyncPostBackError">
</asp:ScriptManager>

<scripttype="text/javascript"language="javascript">

var
本文介绍了ASP.NET AJAX Extensions中的ScriptManager控件,它是实现页面局部更新和客户端脚本生成的关键。文章详细解释了ScriptManager的配置选项及其如何用于错误处理,并通过示例展示了其在实践中的应用。
1万+

被折叠的 条评论
为什么被折叠?



