大家好,又见面了,我是你们的朋友全栈君。
1.1 场景
1.2 背景
2 UpdatePanel 的使用

<
asp:UpdatePanelID
=
“
UpdatePanel1
“
2
UpdateMode=
“
Conditional
“
3
runat=
“
server
“
>
4
<
ContentTemplate
>
5
<
asp:Button ID
=
“
Button1
“
6
Text=
“
RefreshPanel
“
7
runat=
“
server
“
/>
8
</
ContentTemplate
>
9

</
asp:UpdatePanel
>
10
UpdatePanel 控件可以输出为 <div> 元素或 <span> 元素,以在页面中形成一个块或内联的区域,可以设置其 RenderMode 属性为 Block ( 默认,<div>)或 Inline ( <span> ) 来指定。
2.1 指定 UpdatePanel 的内容
包含一个或多个 UpdatePanel 控件的页面在第一次输出时,所有 UpdatePanel 控件中的内容都会被输出并被发送到浏览器。在后来的异步更新中,单个 UpdatePanel 控件中的内容可能会被更新。更新依赖于面板的设置、导致回发的元素以及指定给每个面板的代码。
2.2 指定 UpdatePanel 的触发器
下列示例展示了如何添加一个触发器到 UpdatePanel 面板中去。

<
asp:Button ID
=
“
Button1
“
Text=
“
Refresh Panel
“
runat=
“
server
“
/>

<
asp:ScriptManager ID
=
“
ScriptManager1
“
runat=
“
server
“
/>

<
asp:UpdatePanel ID
=
“
UpdatePanel1
“
UpdateMode
=
“
Conditional
“
runat
=
“
server
“
>
<
Triggers
>
<
asp:AsyncPostBackTrigger ControlID
=
“
Button1
“
/>
</
Triggers
>
<
ContentTemplate
>
<
fieldset
>
<
legend
>
UpdatePanel 内容
</
legend
>
<%=
DateTime.Now.ToString()
%>
</
fieldset
>
</
ContentTemplate
>

</
asp:UpdatePanel
>
触发器由在 UpdatePanel 控件的 <Triggers> 元素中的 <asp:AsyncPostBackTrigger> 元素定义。(如果是在 Visual Studio 中编辑页面,就可以在 UpdatePanel 的属性面板中单击 Triggers 属性后面的省略号按钮打开一个 UpdatePanelTrigger 集合编辑器对话框来创建触发器。)触发器必要的属性是 ControlID ,用它来指定可以导致面板更新的控件的 ID 。
有上例中,虽然按钮没有声明在面板中,但是由于在面板中指定了它为触发器,所以当按钮事件触发时,会产生其被包含中面板中同样的结果,即面板被更新。
触发器控件的事件是可选的,如果没有指定事件,触发器将使用控件的默认事件。例如,对于 Button 控件,默认事件就是 Click 事件。
2.3 在母版页中使用 UpdatePanel
如果在母版页中没有包含 ScriptManager 控件,就必须在包含 UpdatePanel 控件的每个内容页是都要放置一个 ScriptManager 控件,设计的选择依赖于在应用程序中将如何管理客户端脚本。
如果在母版页中包含了 ScriptManager 控件,而在某个内容页中又不打算使用局部页面输出的功能时,必须用程序设置内容中的 ScriptManager 控件的 EnablePartialRendering 为 false 。
2.4 使用嵌套的 UpdatePanel 控件
下列代码展示了如何在一个 UpdatePanel 控件中定义另一个 UpdatePanel 控件。

<%
@ Page Language
=
“
C#
“
%>
2

<!
DOCTYPE html PUBLIC
“
-//W3C//DTD XHTML 1.0 Transitional//EN
“
3
“
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
“
>
4

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

<
head id
=
“
Head1
“
runat
=
“
server
“
>
6
<
title
>
UpdatePanelUpdateMode 示例
</
title
>
7
<
style type
=
“
text/css
“
>
8
div.NestedPanel9


{
10
position: relative;11
margin: 2
%
5
%
2
%
5
%
;
12
}13
</
style
>
14

</
head
>
15

<
body
>
16
<
form id
=
“
form1
“
runat
=
“
server
“
>
17
<
div
>
18
<
asp:ScriptManager ID
=
“
ScriptManager
“
19
runat=
“
server
“
/>
20
<
asp:UpdatePanel ID
=
“
OuterPanel
“
21
UpdateMode=
“
Conditional
“
22
runat=
“
server
“
>
23
<
ContentTemplate
>
24
<
div
>
25
<
fieldset
>
26
<
legend
>
外层 Panel
</
legend
>
27
<
br
/>
28
<
asp:Button ID
=
“
OPButton1
“
29
Text=
“
外层面板按钮
“
30
runat=
“
server
“
/>
31
<
br
/>
32
最后更新在:33
<%=
DateTime.Now.ToString()
%>
34
<
br
/>
35
<
br
/>
36
<
asp:UpdatePanel ID
=
“
NestedPanel1
“
37
UpdateMode=
“
Conditional
“
38
runat=
“
server
“
>
39
<
ContentTemplate
>
40
<
div
class
=
“
NestedPanel
“
>
41
<
fieldset
>
42
<
legend
>
嵌套面板
</
legend
>
43
<
br
/>
44
最后更新在:45
<%=
DateTime.Now.ToString()
%>
46
<
br
/>
47
<
asp:Button ID
=
“
NPButton1
“
48
Text=
“
嵌套面板按钮
“
49
runat=
“
server
“
/>
50
</
fieldset
>
51
</
div
>
52
</
ContentTemplate
>
53
</
asp:UpdatePanel
>
54
</
fieldset
>
55
</
div
>
56
</
ContentTemplate
>
57
</
asp:UpdatePanel
>
58
</
div
>
59
</
form
>
60

</
body
>
61

</
html
>
62
2.5 用程序创建 UpdatePanel 控件
如果 UpdatePanel 控件是程序添加的,只有来自同样命名容器如 UpdatePanel 控件中控件的回发才可以被使用为面板的触发器。
下列代码演示了如何用程序添加 UpdatePanel 控件。

<%
@ Page Language
=
“
C#
“
%>

<!
DOCTYPE html PUBLIC
“
-//W3C//DTD XHTML 1.0 Transitional//EN
“
“
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
“
>

<
script runat
=
“
server
“
>
protected
void
Page_Load(
object
sender, EventArgs e)


{
UpdatePanel up1 =
new
UpdatePanel();
up1.ID =
“
UpdatePanel1
“
;
up1.UpdateMode =
UpdatePanelUpdateMode.Conditional;
Button button1 =
new
Button();
button1.ID =
“
Button1
“
;
button1.Text =
“
Submit
“
;
button1.Click +=
new
EventHandler(Button_Click);
Label label1 =
new
Label();
label1.ID =
“
Label1
“
;
label1.Text =
“
A full page postback occurred.
“
;
up1.ContentTemplateContainer.Controls.Add(button1);
up1.ContentTemplateContainer.Controls.Add(label1);
Page.Form.Controls.Add(up1);
}
protected
void
Button_Click(
object
sender, EventArgs e)


{
((Label)Page.FindControl(“
Label1
“
)).Text
=
“
Panel refreshed at
“
+
DateTime.Now.ToString();
}
</
script
>

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

<
head id
=
“
Head1
“
runat
=
“
server
“
>
<
title
>
UpdatePanel Added Programmatically Example
</
title
>

</
head
>

<
body
>
<
form id
=
“
form1
“
runat
=
“
server
“
>
<
div
>
<
asp:ScriptManager ID
=
“
TheScriptManager
“
runat=
“
server
“
/>
</
div
>
</
form
>

</
body
>

</
html
>

3 UpdatePanel 的关键属性
UpdateMode:
UpdatePanel 控件的内容在下列情形下会更新:
- 如果 UpdateMode 属性设置为 Alwarys 时,UpdatePanel 控件中的内容会在源自页面上任何地方的每个回发时更新。这包括由包含在其他 UpdatePanel 控件中的控件的回发和没有在 UpdatePanel 控件中的回发。
- 如果 UpdatePanel 控件嵌套在另一个 UpdatePanel 控件中时,父面板更新时它也会被更新。
- 如果 UpdateMode 属性被设置为 false 时,且出现下列条件之一时:
- 显式调用 UpdatePanel 控件的 Update() 方法。
- 由 UpdatePanel 控件中的 Triggers 属性定义的触发器控件引起的回送。在这种情况下,控件会显式的触发面板内容的更新。定义为触发器的控件可以在 UpdatePanel 控件的内部也可以在其外部。
- ChildrenAsTriggers 属性设置为 true ,并且是由 UpdatePanel 控件中的子控件导致的回发。在嵌套的 UpdatePanel 控件中的子控件不会引起外层 UpdatePanel 控件的更新,除非显示的定义为触发器。
转载于:https://www.cnblogs.com/hdjjun/archive/2008/06/17/1223647.html
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163514.html原文链接:https://javaforall.net
