VS 2008
本文介绍Microsoft Asp.Net Ajax框架的服务端重要控件
1. ScriptManager
ScriptManager控件被寓为Asp.Net Ajax的大脑,所有的Asp.Net Ajax Enabled Pages都必须包含有且仅有一个ScriptManager控件。
</ asp:ScriptManager >
包含了该控件,页面便会自动引用必需的Core Library脚本文件,并生成一些页面js。
如要现在新建了一个User.js的脚本文件,并需要在页面中注册,则改为:
< Scripts >
< asp:ScriptReference Path = " ~/User.js " />
</ Scripts >
</ asp:ScriptManager >
还可注册客户端js可调用的Web Services:
< Scripts >
< asp:ScriptReference Path = " ~/User.js " />
</ Scripts >
< Services >
< asp:ServiceReference Path = " ~/CountrySvc.asmx " />
</ Services >
</ 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。
< Scripts >
< asp:ScriptReference Path = " Company.js " />
</ Scripts >
< Services >
< asp:ServiceReference Path = " CompanySvc.asmx " />
</ Services >
</ asp:ScriptManagerProxy >
3. UpdatePanel
UpdatePanel定义了部分页面更新的区域,html写在ContentTemplate里面
例:
< ContentTemplate >
< asp:TextBox ID = " txtName " runat = " server " />
< asp:Button ID = " btnShowName " runat = " server " Text = " show name " OnClick = " btnShowName_Click " />
</ ContentTemplate >
</ asp:UpdatePanel >
txtName.Text = "guozhijian";
}
点击btnShowName控件,txtName文本框的值改变了,页面没有回刷
一个页面可以包含多个UpdatePanel:
< ContentTemplate >
< asp:TextBox ID = " txtName " runat = " server " />
< asp:Button ID = " btnShowName " runat = " server " Text = " show name " OnClick = " btnShowName_Click " />
</ ContentTemplate >
</ asp:UpdatePanel >
< asp:UpdatePanel ID = " uPanel2 " runat = " server " >
< ContentTemplate >
< asp:TextBox ID = " txtAge " runat = " server " />
< asp:Button ID = " btnShowAge " runat = " server " Text = " show age " OnClick = " btnShowAge_Click " />
</ ContentTemplate >
</ asp:UpdatePanel >
txtName.Text = "guozhijian";
}
protected void btnShowAge_Click( object sender, EventArgs e) {
txtAge.Text = "26";
}
触发UpdatePanel更新的控件可以至于UpdatePanel外部,这是需要设置UpdatePanle的 UpdateMode="Conditional",并设置UpdatePanel的<Triggers>:
< ContentTemplate >
< asp:TextBox ID = " txtName " runat = " server " />
</ ContentTemplate >
< Triggers >
< asp:AsyncPostBackTrigger ControlID = " btnShowName " />
</ Triggers >
</ asp:UpdatePanel >
< asp:Button ID = " btnShowName " runat = " server " Text = " show name " OnClick = " btnShowName_Click " />
如果一个页面包含多个UpdatePanel,默认情况下如果任何一个UpdatePanel被触发更新,那么都会触发所有UpdatePanel的更新,为避免这一情况发生,建议总是设置UpdatePanel的UpdateMode="Conditional“,指定各自的AsyncPostBackTrigger。
4. Timer
Timer控件用于创建一个定时执行的任务,例:在一个TextBox控件中显示当前时间,每秒刷一次
< ContentTemplate >
< asp:TextBox ID = " txtTime " runat = " server " />
</ ContentTemplate >
< Triggers >
< asp:AsyncPostBackTrigger ControlID = " timer1 " />
</ Triggers >
</ asp:UpdatePanel >
< asp:Timer ID = " timer1 " OnTick = " timer1_Tick " runat = " server " Interval = " 1000 " />
txtTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
5. Exception Handler
如何异步回刷请求提交后,出现异常,那么在客户端会有一个alert提示框提示异常信息
throw new Exception("没有取到名字");
}
如果,需要对页面的异步回刷请求做统一异常处理,那么可以订阅ScirptManager的AsyncPostBackError
{
ScriptManager mgr = ScriptManager.GetCurrent(this.Page);
mgr.AsyncPostBackError += new EventHandler<AsyncPostBackErrorEventArgs>(mgr_AsyncPostBackError);
}
void mgr_AsyncPostBackError( object sender, AsyncPostBackErrorEventArgs e) {
//
ScriptManager.GetCurrent(this.Page).AsyncPostBackErrorMessage = "we are sorry";
}
当ScriptManager的AsyncPostBackError事件触发时,设置 AsyncPostBackErrorMessage
Reference from : http://www.cnblogs.com/guozhijian/archive/2008/02/17/1071242.html