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

2 UpdatePanel 的使用

<
asp:UpdatePanelID
=
“
UpdatePanel1
“
2

=
“
Conditional
“
3

=
“
server
“
>
4

<
ContentTemplate
>
5

<
asp:Button ID
=
“
Button1
“
6

=
“
RefreshPanel
“
7

=
“
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
“

=
“
Refresh Panel
“

=
“
server
“
/>

<
asp:ScriptManager ID
=
“
ScriptManager1
“

=
“
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

9



{
10

11

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

=
“
server
“
/>
20

<
asp:UpdatePanel ID
=
“
OuterPanel
“
21

=
“
Conditional
“
22

=
“
server
“
>
23

<
ContentTemplate
>
24

<
div
>
25

<
fieldset
>
26

<
legend
>
外层 Panel
</
legend
>
27

<
br
/>
28

<
asp:Button ID
=
“
OPButton1
“
29

=
“
外层面板按钮
“
30

=
“
server
“
/>
31

<
br
/>
32

33

<%=
DateTime.Now.ToString()
%>
34

<
br
/>
35

<
br
/>
36

<
asp:UpdatePanel ID
=
“
NestedPanel1
“
37

=
“
Conditional
“
38

=
“
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

=
“
嵌套面板按钮
“
49

=
“
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)



{

=
new
UpdatePanel();

=
“
UpdatePanel1
“
;

=
UpdatePanelUpdateMode.Conditional;

=
new
Button();

=
“
Button1
“
;

=
“
Submit
“
;

+=
new
EventHandler(Button_Click);

=
new
Label();

=
“
Label1
“
;

=
“
A full page postback occurred.
“
;






protected
void
Button_Click(
object
sender, EventArgs e)



{

“
Label1
“
)).Text
=
“
Panel refreshed at
“
+



</
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
“

=
“
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