以下文章前半转自王磊,后半加自己笔记:
引用:
新瓶旧酒 ASP.NET AJAX(1) - 简单地过一下每个控件(ScriptManager、ScriptManagerProxy、UpdatePanel、 UpdateProgress和Timer)
作者:webabcd 介绍 ASP.NET AJAX就5个控件,分别是ScriptManager、ScriptManagerProxy、UpdatePanel、UpdateProgress和Timer。先简单地过一下。 关键 1、ScriptManager 和ScriptManagerProxy ·一个页只能有一个ScriptManager(包含了所有脚本资源),要放到任何用到AJAX的控件的前面。 ·如果把它放到母版页,而内容页需要与其不同的配置的话,则应在内容页使用ScriptManagerProxy。 ·ScriptManager默认EnablePartialRendering="true"。 ·ScriptManager的 AllowCustomErrorsRedirect="true"的时候,出现异常就会转到web.config里customErrors中defaultRedirect所指的地址。 2、 UpdatePanel ·UpdatePanel内放置需要被刷新的控件,如果是其内部控件事件导致其刷新,则不用另外做什么设置,因为UpdatePanel默认ChildrenAsTriggers="true"。 ·如果是UpdatePanel外部控件导致其刷新的话,则应设置 Triggers。 ·在Triggers内,如果AsyncPostBackTrigger未设置EventName,则为其指定控件的默认事件。 ·UpdatePanel默认UpdateMode="Always",需要的话应设置UpdateMode="Conditional"。 ·RenderMode="Block"对应div;RenderMode="Inline"对应span 3、UpdateProgress ·默认,任何回发,当有延迟的时候则显示UpdateProgress里的ProgressTemplate。 ·要与某UpdatePanel关联则设置 AssociatedUpdatePanelID属性。 ·DynamicLayout为true则用“display:none;”隐藏;DynamicLayout为false则用 “visibility:hidden;”隐藏。 ·默认情况下,例如有2个异步回发,如果第1个还没有执行完毕就执行第2个的话,则会先取消第1个异步回发。 4、Timer ·Interval:间隔时间,单位(毫秒);每一个间隔时间后将触发 Tick事件。 ·Timer要放在其所刷新的UpdatePanel内部;放外面的话要设置UpdatePanel的Triggers。 5、其它 ·要在UpdatePanel中使用Validator的话,请参看http://blogs.msdn.com/mattgi/archive/2007/01/23/asp-net-ajax-validators.aspx ·内容页获取母版页的ScriptManager:ScriptManager.GetCurrent(Page) ·后置代码处对UpdatePanel编程,例: ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(this.Button1); ScriptManager.GetCurrent (this).RegisterPostBackControl(this.Button2); this.UpdatePanel1.Update(); ·Internet Explorer Developer Toolbar下载 ·Fiddler下载 ·Web Development Helper下载 示例 1、最简单的示例
<%
@ Page Language = " C# " MasterPageFile = " ~/Site.master " AutoEventWireup = " true " CodeFile = " Sample.aspx.cs " Inherits= " Overview_Sample " Title = " 最简单的示例 "
%>
<
asp:Content
ID
="Content1"
ContentPlaceHolderID
="ContentPlaceHolder1"
runat
="Server"
>
<
ul
>
<
li
>
之前要有ScriptManager(包含了所有脚本资源),我把它放到母版页了。内容页如需不同配置则应使用ScriptManagerProxy。
li
>
<
li
>
最简单的示例,拖个UpdatePanel进来。在 UpdatePanel内拖个GridView,并设置其数据源即可。
li
>
ul
>
<
asp:UpdatePanel
ID
="UpdatePanel1"
runat
="server"
>
<
ContentTemplate
>
<
asp:GridView
ID
="GridView1"
runat
="server"
AllowPaging
="True"
AllowSorting
="True"
DataSourceID
="SqlDataSource1"
>
<
Columns
>
& nbsp;
<
asp:CommandField
ShowEditButton
="True"
ShowSelectButton
="True"
/>
Columns
>
asp:GridView
>
ContentTemplate
>
asp:UpdatePanel
>
<
asp:SqlDataSource
ID
="SqlDataSource1"
runat
="server"
ConnectionString
="<% $ ConnectionStrings:connstr %>"
DeleteCommand
="DELETE FROM [Products] WHERE [ProductID] = @ProductID"
InsertCommand
="INSERT INTO [Products] ([ProductName], [QuantityPerUnit], [UnitPrice], [Discontinued]) VALUES (@ProductName, @QuantityPerUnit, @UnitPrice, @Discontinued)"
SelectCommand
="SELECT [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice], [Discontinued] FROM [Products]"
UpdateCommand
="UPDATE [Products] SET [ProductName] = @ProductName, [QuantityPerUnit] = @QuantityPerUnit, [UnitPrice] = @UnitPrice, [Discontinued] = @Discontinued WHERE [ProductID] = @ProductID"
>
<
DeleteParameters
>
<
asp:Parameter
Name
="ProductID"
Type
="Int32"
/>
DeleteParameters
>
<
UpdateParameters
>
<
asp:Parameter
Name
="ProductName"
Type
="String"
/>
<
asp:Parameter
Name
="QuantityPerUnit"
Type
="String"
/>
<
asp:Parameter
Name
="UnitPrice"
Type
="Decimal"
/>
<
asp:Parameter
Name
="Discontinued"
Type
="Boolean"
/>
<
asp:Parameter
Name
="ProductID"
Type
="Int32"
/>
UpdateParameters
>
<
InsertParameters
>
<
asp:Parameter
Name
="ProductName"
Type
="String"
/>
<
asp:Parameter
Name
="QuantityPerUnit"
Type
="String"
/>
<
asp:Parameter
Name
="UnitPrice"
Type
="Decimal"
/>
<
asp:Parameter
Name
="Discontinued"
Type
="Boolean"
/>
InsertParameters
>
asp:SqlDataSource
>
asp:Content
>
2、UpdatePanel
<%
@ Page Language = " C# " MasterPageFile = " ~/Site.master " AutoEventWireup = " true " CodeFile = " UpdatePanel.aspx.cs " Inherits= " Overview_UpdatePanel " Title = " UpdatePanel "
%>
<
asp:Content
ID
="Content1"
ContentPlaceHolderID
="ContentPlaceHolder1"
runat
="Server"
>
<
ul
>
<
li
>
UpdatePanel内放置需要被刷新的控件,如果是其内部控件事件导致其刷新,则不用另外做什么设置,因为UpdatePanel默认ChildrenAsTriggers="true"。
li
>
<
li
>
如果是UpdatePanel外部控件导致其刷新的话,则应设置Triggers。
li
>
<
li
>
在Triggers内,如果AsyncPostBackTrigger 未设置EventName,则为其指定控件的默认事件。
li
>
<
li
>
UpdatePanel默认UpdateMode="Always",需要的话应设置UpdateMode="Conditional"。
li
>
<
li
>
RenderMode="Block"对应div; RenderMode="Inline"对应span
li
>
ul
>
<
asp:UpdatePanel
ID
="UpdatePanel1"
runat
="server"
>
<
contenttemplate
>
<
fieldset
>
<
legend
>
我在UpdatePanel里
legend
>
<
asp:Label
ID
="Label1"
runat
="server"
Text
="我是 Label"
>
asp:Label
>
fieldset
>
contenttemplate
>
<
triggers
>
<%
-- 如果没设置 EventName,则取默认事件,Button的默认事件为Click --
%>
<
asp:AsyncPostBackTrigger
ControlID
="Button1"
/>
triggers
>
asp:UpdatePanel
>
<
p
>
p
>
<
fieldset
>
<
legend
>
我在UpdatePanel外
legend
>
<
asp:Button
ID
="Button1"
runat
="server"
Text
="按钮"
OnClick
="Button1_Click"
/>
fieldset
>
<
p
>
p
>
<%
-- 嵌套UpdatePanel --
%>
<
asp:UpdatePanel
ID
="UpdatePanel2"
runat
="server"
UpdateMode
="Conditional"
>
<
contenttemplate
>
<
fieldset
>
<
legend
>
外围UpdatePanel
legend
>
<
asp:Label
ID
="Label2"
runat
="server"
Text
="我是 Label"
>
asp:Label
>
<
asp:Button
ID
="Button2"
runat
="server"
Text
="按钮"
OnClick
="Button2_Click"
/>
<
asp:UpdatePanel
ID
="UpdatePanel3"
runat
="server"
>
& nbsp;
<
ContentTemplate
>
& nbsp;
<
fieldset
>
& nbsp;
<
legend
>
嵌套UpdatePanel
legend
>
& nbsp;
<
asp:Label
ID
="Label3"
runat
="server"
Text
="我是Label"
>
asp:Label
>
& nbsp;
<
asp:Button
ID
="Button3"
runat
="server"
Text
="按钮"
OnClick
="Button3_Click"
/>
& nbsp;
fieldset
>
& nbsp;
ContentTemplate
>
asp:UpdatePanel
>
fieldset
>
contenttemplate
>
asp:UpdatePanel
>
asp:Content
>
3、UpdateProgress
<%
@ Page Language = " C# " MasterPageFile = " ~/Site.master " AutoEventWireup = " true " CodeFile = " UpdateProgress.aspx.cs " Inherits= " Overview_UpdateProgress " Title = " UpdateProgress "
%>
<
asp:Content
ID
="Content1"
ContentPlaceHolderID
="ContentPlaceHolder1"
runat
="Server"
>
<
ul
>
<
li
>
默认,任何回发,当有延迟的时候则显示 UpdateProgress里的ProgressTemplate。
li
>
<
li
>
要与某UpdatePanel关联则设置 AssociatedUpdatePanelID属性。
li
>
<
li
>
DynamicLayout为true则用“display:none; ”隐藏;DynamicLayout为false则用“visibility:hidden;”隐藏。
li
>
<
li
>
默认情况下,例如有2个异步回发,如果第1 个还没有执行完毕就执行第2个的话,则会先取消第1个异步回发。
li
>
ul
>
<
asp:UpdatePanel
ID
="UpdatePanel1"
runat
="server"
>
<
ContentTemplate
>
<
fieldset
>
<
legend
>
UpdatePanel1
legend
>
<
asp:Label
ID
="Label1"
runat
="server"
Text
="Label1"
>
asp:Label
>
<
br
/>
<
asp:Button
ID
="Button1"
runat
="server"
Text
="Button"
OnClick
="Button1_Click"
/>
fieldset
>
ContentTemplate
>
asp:UpdatePanel
>
<
asp:UpdateProgress
ID
="UpdateProgress1"
runat
="server"
AssociatedUpdatePanelID
="UpdatePanel1"
DynamicLayout
="false"
>
<
ProgressTemplate
>
<
p
>
UpdatePanel1更新中
p
>
ProgressTemplate
>
asp:UpdateProgress
>
<
p
>
p
>
<
asp:UpdatePanel
ID
="UpdatePanel2"
runat
="server"
>
<
ContentTemplate
>
<
fieldset
>
<
legend
>
UpdatePanel2
legend
>
<
asp:Label
ID
="Label2"
runat
="server"
Text
="Label2"
>
asp:Label
>
<
br
/>
<
asp:Button
ID
="Button2"
runat
="server"
Text
="Button"
OnClick
="Button2_Click"
/>
fieldset
>
ContentTemplate
>
asp:UpdatePanel
>
<
asp:UpdateProgress
ID
="UpdateProgress2"
runat
="server"
AssociatedUpdatePanelID
="UpdatePanel2"
DynamicLayout
="true"
>
<
ProgressTemplate
>
<
p
>
UpdatePanel2更新中
p
>
ProgressTemplate
>
asp:UpdateProgress
>
<
p
>
p
>
<
asp:UpdatePanel
ID
="UpdatePanel3"
runat
="server"
>
<
ContentTemplate
>
<
fieldset
>
<
legend
>
UpdatePanel3
legend
>
<
asp:Label
ID
="Label3"
runat
="server"
Text
="Label3"
>
asp:Label
><
br
/>
<
asp:Button
ID
="Button3"
runat
="server"
Text
="Button"
OnClick
="Button3_Click"
/>
fieldset
>
ContentTemplate
>
asp:UpdatePanel
>
<
asp:UpdateProgress
ID
="UpdateProgress3"
runat
="server"
>
<
ProgressTemplate
>
有延迟我就更新
ProgressTemplate
>
asp:UpdateProgress
>
asp:Content
>
4、Timer
<%
@ Page Language = " C# " MasterPageFile = " ~/Site.master " AutoEventWireup = " true " CodeFile = " Timer.aspx.cs " Inherits= " Overview_Timer " Title = " Timer "
%>
<
asp:Content
ID
="Content1"
ContentPlaceHolderID
="ContentPlaceHolder1"
runat
="Server"
>
<
ul
>
<
li
>
Interval:间隔时间,单位(毫秒);每一个间隔时间后将触发Tick事件
li
>
<
li
>
Timer要放在其所刷新的UpdatePanel内部;放外面的话要设置UpdatePanel的Triggers。
li
>
ul
>
<
asp:UpdatePanel
ID
="UpdatePanel1"
runat
="server"
>
<
ContentTemplate
>
<
fieldset
>
<
legend
>
UpdatePanel1
legend
>
<
p
>
& nbsp;内部Timer & nbsp;
<
asp:Timer
ID
="Timer1"
runat
="server"
OnTick
="Timer1_Tick"
Interval
="1000"
>
& nbsp;
asp:Timer
>
p
>
<
asp:Label
ID
="Label1"
runat
="server"
Text
="Label"
>
asp:Label
>
fieldset
>
ContentTemplate
>
asp:UpdatePanel
>
asp:Content
>
注:以上示例涉及到后置代码的,其作用都是用来刷新相关的Label的,所以略掉了。
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
对其中的属性进行进一步解释:
UpdateMode有两个值:always 和 conditional,通过这两个值可以用来控制UpdatePanel的刷新方式。 1. always :如果将UpdateMode属性设为"always",则每次发生页面回发的时候,该UpdatePanel都会刷新。这里的页面回发包括由其他UpdatePanel里的控件或不在UpdatePanel里的控件产生的回发。接着昨天的例子稍做一下改动:
always UpdatePanel: CodeFile:
protected void btnShowTime_Click(object sender, EventArgs e) { lblTime.Text = DateTime.Now.ToString(); lblAlwaysTime.Text = DateTime.Now.ToString(); } 此时如果点击"ShowTime"按钮,两个UpdatePanel里的Label会同时更新,但如果将第二个UpdatePanel的UpdateMode设为conditional,她里面的Label就不会更新了。 2.conditional:若UpdateMode被设为conditional,则该UpdatePanel会在下列情况被刷新: (1) 由触发器引起的刷新,如下所示:
always UpdatePanel: 通过在UpdatePanel里设置触发器可以指定特定的按钮来刷新该UpdatePanel.上例中就给UpdatePanel设置了一个触发器,该触发器关联的控件是"btnShowTime",关联的事件是 "Click"。一个UpdatePanel可以设置多个触发器,关于触发器更深一步的知识我想以后会慢慢了解。 (2) 调用UpdatePanel的Update()方法:
protected void btnShowTime_Click(object sender, EventArgs e) { lblTime.Text = DateTime.Now.ToString(); lblAlwaysTime.Text = DateTime.Now.ToString(); up2.Update(); } (3) 当该UpdatePanel在其他UpdatePanel里面,并且父容器更新的时候:
always UpdatePanel:
(4) 当 ChildrenAsTriggers 属性为true,UpdatePanel 内的控件产生回发时,其实之前的例子就和这种情况差不多。因为默认情况下ChildrenAsTriggers的值为true ,可以省略,此是UpdateMode不管为always还是conditional效果是一样的。需要注意的是子UpdatePanel里的控件并不会引起父UpdatePanel的更新,需要显示的申明为触发器或者调用父UpdatePanel的Update()方法。 注意:如果ChildrenAsTriggers设为false,则UpdateMode的值必须设为conditional,否则就会抛出异常。默认情况下UpdateMode的值为always。////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
功能强大的UpdatePanel控件:
改进后的UpdatePanel使页面部分更新(Partial-Page Updates)实现起来非常容易。 要想在已有web页面或新建页面中加入部分更新内容,都十分容易,下面几个步骤:
<1>在页面中加入ScriptManager控件。并保证ScriptManager控件的EnablePartialRendering属性值为 true。若EnablePartialRendering=false,那么下面所做的对页面部分更新的任何设置都不能实现。EnablePartialRendering的默认值是true,不作修改就行。
Code highlighting produced by Actipro CodeHighlighter (freeware)
-->
<2>把UpdatePanel控件加到页面中。在 中加入想部分更新的内容就行了。
In UpdatePanel UpdatePanel content refreshed at <%=DateTime.Now.ToString() %>
为了对比,在UpdatePanel外面加一行代码
Out of UpdatePanel,refreshed at <%=DateTime.Now.ToString() %>
这样部分更新功能就实现了,或许都不敢相信。
看看效果吧。
两部分更新时间不一样!
UpdatePanel控件的功能是很强大的。这是最简单的应用。
部分更新时,提交给服务器的数据跟一般的postback没有什么区别,所有数据包括viewstate中的数据被传回服务器。不同的地方在于从服务器只返回部分更新部分的数据。由于UpdatePanel控件的引入,postback被分为两种,asynchronous postback和normal postback,asynchronous postback引起UpdatePanel的更新,normal postback引发整个页面的更新。使用ScriptManager的IsInAsyncPostBack属性可以判断回传的类型。
介绍一下UpdatePanel的属性。
<1>Triggers
有两种AsyncPostBackTrigger,PostBackTrigger。 AsyncPostBackTrigger
来指定某个控件的某个事件引发异步回传(asynchronous postback),即部分更新。属性有ControlID和EventName。分别用来指定控件ID和控件事件,若没有明确指定EventName的值,则自动采用控件的默认值,比如button就是click。把ContorlID设为UpdatePanel外部控件的ID,可以使外部控件控制 UpdatePanel的更新。
PostBackTrigger
来指定UpdatePanel内的某个控件引发整个页面的更新(normal postback)。
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/
--> <2>UpdateMode
有两个值:Always,Conditional。总是更新,有条件更新。
确定当asynchronous postbacks发生时,是否总是更新。若页面中只有一个UpdatePanel控件,这个值好像没有什么意义。但是当页面中存在多个 UpdatePanel,或者UpdatePanel中包含UpdatePanel的复杂情况时,这个值的设定就可以使各个UpdatePanel在各种合适时机更新。 <3>ChilderAsTriggers bool值,默认是true。若设为false,则UpdatePanel的子控件引发异步回传(asynchronous postback),但是不更新当前UpdatePanel(在多个UpdatePanel的页面中发现的)。这里比较难于理解,甚至我理解的是错误的。请高手指点。
该属性只在UpdateMode=Conditional条件下有意义。右UpdateMode为Always,ChilderAsTriggers=false就则引发异常。
另外UpdatePanel还提供了一个方法Update(),可以通过代码控件部分更新。
下面给个代码,使用了这些属性。
<%@ Page Language="C#" %>
Untitled Page
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UpdatePanel 学习之 ChildrenAsTriggers
上一篇讲述了如何使用UpdatePanel的UpdateMode属性限制只有本UpdatePanel里面的控件所引发的更新才使UpdatePanel更新有效。那么与这个属性很相关的一个属性就是ChildrenAsTriggers 。ChildrenAsTriggers 属性有两个可选值,一个是false,一个是true。如果设为false,则说明此UpdatePanel不进行自动更新。但可以手动更新,如果设置为true,则这个UpdatePanel自动更新。上一篇说到把UpdatePanel的UpdateMode设置为Always,则不管是页面中任何控件引发的事件,都会导致UpdatePanel进行更新,那么如果把UpdatePanel的ChildrenAsTriggers 值设置为false的话,则说UpdatePanel不进行自动更新,那么这样不是产生矛盾了吗,一个要更新,一个又阻止更新,的确,这样的设置方法是不行的。也就是说UpdateMode=Always, ChildrenAsTriggers =false这种组合是不行的,如果ChildrenAsTriggers =false,则UpdateMode必须设置为conditional。 为了说明效果,我再引用上一节所引用的例子,如下所示:
Code
1 <% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default2.aspx.cs " Inherits = " Default2 " %> 2 3 DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > 4 5 < html xmlns ="http://www.w3.org/1999/xhtml" > 6 < head runat ="server" > 7 < title > 无标题页 title > 8 head > 9 < body > 10 < form id ="form1" runat ="server" > 11 < div > 12 < asp:ScriptManager ID ="ScriptManager1" runat ="server" > 13 asp:ScriptManager > 14 < asp:UpdatePanel ID ="UpdatePanel1" runat ="server" > 15 < ContentTemplate > 16 <% = DateTime.Now %> 17 < asp:Button ID ="Button1" runat ="server" Text ="Button" /> 18 ContentTemplate > 19 asp:UpdatePanel > 20 < hr /> 21 < asp:UpdatePanel ID ="UpdatePanel2" UpdateMode ="Conditional" ChildrenAsTriggers ="false" runat ="server" > 22 < ContentTemplate > 23 <% = DateTime.Now %> 24 < asp:Button ID ="Button2" runat ="server" Text ="Button" /> 25 ContentTemplate > 26 asp:UpdatePanel > 27 div > 28 form > 29 body > 30 html > 31
上面所显示的代码中,我们可以看到,也是放置了两个UpdatePanel,也是显示时间,但是第二个UpdatePanel设置了ChildrenASTriggers为False,则这样,在执行的时候,第二个updatepanel不管是点哪个按钮,都不会自动刷新,因为这样的设置,如果把updateMode,设置为always,则会出现这样的错误:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UpdataPanel学习之 RenderMode
RenderMode;它的意思是包含在updatepanel里面的东西用什么来包含。具体一点就是,不管你用的是什么服务器控件,那么最后生成的客户端代码总是一个一个的HTML标签。比如说一个,最后生成的页面代码里面就会变成。对于updatepanel来说,他也会转化成一个HTML标签,到底转化成什么样的HTML标签呢,那么就要看RenderMode的属性设置了。它有两个值可以选择。一个是block,一个是Inline。从字面意思上体会了一下,用不太恰当的说法就是一个是块,一个是行。为什么这样说,那么我们看一下他的代码就可以明白了。我就举一下赵老师视频里面的代码来说明一个问题。