VS 2008
本文介绍Microsoft Asp.Net Ajax框架的服务端重要控件
1. ScriptManager
ScriptManager控件被寓为Asp.Net Ajax的大脑,所有的Asp.Net Ajax Enabled Pages都必须包含有且仅有一个ScriptManager控件。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:ScriptManager ID
=
"
scriptMgr
"
runat
=
"
server
"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
asp:ScriptManager
>
包含了该控件,页面便会自动引用必需的Core Library脚本文件,并生成一些页面js。
如要现在新建了一个User.js的脚本文件,并需要在页面中注册,则改为:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:ScriptManager ID
=
"
scriptMgr
"
runat
=
"
server
"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
Scripts
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:ScriptReference Path
=
"
~/User.js
"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
Scripts
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
asp:ScriptManager
>
还可注册客户端js可调用的Web Services:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:ScriptManager ID
=
"
scriptMgr
"
runat
=
"
server
"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
Scripts
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:ScriptReference Path
=
"
~/User.js
"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
Scripts
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
Services
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:ServiceReference Path
=
"
~/CountrySvc.asmx
"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
Services
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
asp:ScriptManager
>
这样,ScriptManager会为Web Service生成一个客户端脚本的web service访问代理,供客户端调用。
2. ScriptMangerProxy
一个页面只能有一个ScriptManager控件,但,如果页面使用MasterPage, ContentPage, 通常ScriptManager会置于MasterPage,在那里注册通用的js或web service,那么,在Content Page中可能也需要注册只需要当前页面调用的js或Web Service,那么就需要用到ScriptManagerProxy控件。
ScriptManagerProxy继承自ScriptManager。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:ScriptManagerProxy ID
=
"
sriptMgrPxy
"
runat
=
"
server
"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
Scripts
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:ScriptReference Path
=
"
Company.js
"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
Scripts
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
Services
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:ServiceReference Path
=
"
CompanySvc.asmx
"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
Services
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
asp:ScriptManagerProxy
>
3. UpdatePanel
UpdatePanel定义了部分页面更新的区域,html写在ContentTemplate里面
例:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:UpdatePanel ID
=
"
uPanel1
"
runat
=
"
server
"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
ContentTemplate
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:TextBox ID
=
"
txtName
"
runat
=
"
server
"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:Button ID
=
"
btnShowName
"
runat
=
"
server
"
Text
=
"
show name
"
OnClick
=
"
btnShowName_Click
"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
ContentTemplate
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
asp:UpdatePanel
>
点击btnShowName控件,txtName文本框的值改变了,页面没有回刷
一个页面可以包含多个UpdatePanel:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:UpdatePanel ID
=
"
uPanel1
"
runat
=
"
server
"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
ContentTemplate
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:TextBox ID
=
"
txtName
"
runat
=
"
server
"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:Button ID
=
"
btnShowName
"
runat
=
"
server
"
Text
=
"
show name
"
OnClick
=
"
btnShowName_Click
"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
ContentTemplate
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
asp:UpdatePanel
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:UpdatePanel ID
=
"
uPanel2
"
runat
=
"
server
"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
ContentTemplate
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:TextBox ID
=
"
txtAge
"
runat
=
"
server
"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:Button ID
=
"
btnShowAge
"
runat
=
"
server
"
Text
=
"
show age
"
OnClick
=
"
btnShowAge_Click
"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
ContentTemplate
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
asp:UpdatePanel
>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
protected
void
btnShowName_Click(
object
sender, EventArgs e)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
txtName.Text = "guozhijian";
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
protected
void
btnShowAge_Click(
object
sender, EventArgs e)
{
txtAge.Text = "26";
}
触发UpdatePanel更新的控件可以至于UpdatePanel外部,这是需要设置UpdatePanle的 UpdateMode="Conditional",并设置UpdatePanel的<Triggers>:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:UpdatePanel ID
=
"
uPanel1
"
runat
=
"
server
"
UpdateMode
=
"
Conditional
"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
ContentTemplate
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:TextBox ID
=
"
txtName
"
runat
=
"
server
"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
ContentTemplate
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
Triggers
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:AsyncPostBackTrigger ControlID
=
"
btnShowName
"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
Triggers
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
asp:UpdatePanel
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:Button ID
=
"
btnShowName
"
runat
=
"
server
"
Text
=
"
show name
"
OnClick
=
"
btnShowName_Click
"
/>
如果一个页面包含多个UpdatePanel,默认情况下如果任何一个UpdatePanel被触发更新,那么都会触发所有UpdatePanel的更新,为避免这一情况发生,建议总是设置UpdatePanel的UpdateMode="Conditional“,指定各自的AsyncPostBackTrigger。
4. Timer
Timer控件用于创建一个定时执行的任务,例:在一个TextBox控件中显示当前时间,每秒刷一次
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:UpdatePanel ID
=
"
uPanel1
"
runat
=
"
server
"
UpdateMode
=
"
Conditional
"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
ContentTemplate
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:TextBox ID
=
"
txtTime
"
runat
=
"
server
"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
ContentTemplate
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
Triggers
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:AsyncPostBackTrigger ControlID
=
"
timer1
"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
Triggers
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
asp:UpdatePanel
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:Timer ID
=
"
timer1
"
OnTick
=
"
timer1_Tick
"
runat
=
"
server
"
Interval
=
"
1000
"
/>
5. Exception Handler
如何异步回刷请求提交后,出现异常,那么在客户端会有一个alert提示框提示异常信息
如果,需要对页面的异步回刷请求做统一异常处理,那么可以订阅ScirptManager的AsyncPostBackError
当ScriptManager的AsyncPostBackError事件触发时,设置 AsyncPostBackErrorMessage
Reference from : http://www.cnblogs.com/guozhijian/archive/2008/02/17/1071242.html