4.当移动光标到“OnMouseOver事件演示“按钮时,该按钮背景色变为淡绿色:. h% ?) @; z! L& u
5.当光标移动到“OnMouseOut事件演示“按钮时,该按钮的字体变为黑体,但是我们需观察的却是再移开光标后,字体是否恢复正常,结论是会的,这里只给出了移动到该按钮时的画面,移开后的画面由于和开始画面一样,就不演示了。
* v0 r6 G/ ]8 a3 R9 F2 o2.2.4 复选控件' \: I6 y5 R+ R+ U. @0 s/ s* j
在日常信息输入中,我们会遇到这样的情况,输入的信息只有两种可能性(例如:性别、婚否之类),如果采用文本输入的话,一者输入繁琐,二者无法对输入信息的有效性进行控制,这时如果采用复选控件(CheckBox),就会大大减轻数据输入人员的负担,同时输入数据的规范性得到了保证。
$ E: X% l5 y$ X4 _, GCheckBox的使用比较简单,主要使用id属性和text属性。Id属性指定对复选控件实例的命名,Text属性主要用于描述选择的条件。另外当复选控件被选择以后,通常根据其Checked属性是否为真来判断用户选择与否。
, r" L. V4 {9 n: `/ B, _例如:使用复选控件
5 v; c a: P2 N5 ~& @…9 m2 }$ a$ Z X( f- Q4 }
<asp:CheckBox id=”chkbox1” text=”中国人” runat=server/>' w; r4 h1 \, D- e* F/ i: \# b# h
…* q7 x8 s \0 L. B# e1 `2 L& g9 L
判断条件满足否:- E$ j5 M, R; v3 d, V3 _ b( O
…/ @6 Q! F1 p: J/ a! v5 d
If chkbox1.Checked=True# ?( u/ h/ H& P* }; V
LblTxt.text=”是中国人”4 t, A- G1 Q1 Z7 z" m; x9 S
Else& j) i2 f# U: ?# x+ X( u) \5 d
LblTxt.text=”不是中国人”. _" ?" y1 _% g! Y4 G" x
End If
8 g' W8 r* Q; |1 h…
2 ]* ]- ]1 c9 j: l3 ]2.2.5 单选控件
& B8 ]5 L; ]4 v. c- G- s: C$ Y使用单选控件的情况跟使用复选控件的条件差不多,区别的地方在于,单选控件的选择可能性不一定是两种,只要是有限种可能性,并且只能从中选择一种结果,在原则上都可以用单选控件(RadioButton)来实现。
& i" Z: Y5 \2 o$ b单选控件主要的属性跟复选控件也类似,也有id属性、text属性,同样也依靠Checked属性来判断是否选中,但是与多个复选控件之间互不相关不同,多个单选控件之间存在着联系,要么是同一选择中的条件,要么不是。所以单选控件多了一个GroupName属性,它用来指明多个单选控件是否是同一条件下的选择项,GroupName相同的多个单选控件之间只能有一个被选中。
) {! A$ W7 c9 V! {8 \5 ^ \" Q例如:对单选控件的使用 N0 E" d5 }: ~1 V$ E) ^; W- I& K; M
…
* q) s: ] b5 { [ 年龄选择: _( C& Y" N& Q: v, x& V) K9 Y! S
<asp:RadioButton id=”rbtn11” text=”20岁以下” GroupName=”group1” runat=server /><br>
, z! j* K5 _2 r- p' }<asp:RadioButton id=”rbtn12” text=”20-30岁” GroupName=”group1” runat=server /><br>8 i, t+ O, [2 y, M3 c
<asp:RadioButton id=”rbtn13” text=”30-40岁” GroupName=”group1” runat=server /><br>
( [- E1 I; `! X9 L$ c5 n<asp:RadioButton id=”rbtn14” text=”40岁以上” GroupName=”group1” runat=server /><br>
* i3 e2 S4 V; P工作收入:
3 W' h9 P5 ~1 S7 m- J<asp:RadioButton id=”rbtn21” text=”1000元以下” GroupName=”group2” runat=server /><br>
4 D* A# i, D0 G$ f, n9 y<asp:RadioButton id=”rbtn22” text=”1000-2000元” GroupName=”group2” runat=server/><br>4 V1 H( L( x/ q/ y1 ]
<asp:RadioButton id=”rbtn23” text=”2000元以上” GroupName=”group2” runat=server />
# n* {& A; U! s9 W…
) |( k# H2 Q+ s6 T; M5 Q- J对选择条件的判断:
3 J. ~2 B; a' R; G, E…
. }' w) ~. i& n7 p; O5 @# rIf rbtn11.Checked = True
, S9 g6 N' P* F& yLblResult1.text=”20岁以下”
1 O5 r2 c4 X. A3 PElse if rbtn12.Checked = True
) O& e& D8 S* m/ `3 ZLblResult1.text=”20-30岁”1 Q! c1 o( S: P
Else if rbtn13.Checked = True2 ^- V) Z8 R9 s, D; N* G7 {
LblResult1.text=”30-40岁”, c# `$ E3 w- Q) i `
Else2 B" d2 w G8 Q$ o, p# s) n; s
LblResult1.text=”40岁以上”
- n# |. P) r" y9 }7 YEnd If
% F2 J- v3 Q- r: ZIf rbtn21.Checked = True
6 N" E @& H ~0 @1 tLblResult2.text=”1000元以下” ] u& Q6 J: S$ w
Else if rbtn22.Checked = True
6 o2 Q/ w/ K& i; W! d. TLblResult2.text=”1000-2000元”
, L8 u5 h- i9 `Else8 a; p) G6 q, ~, }7 c
LblResult.text=”2000元以上”
6 E, a9 E" [/ L) J$ z& A6 k- Y) fEnd If5 H" T/ H) f, @0 d/ M# p5 K$ A; `
…
: G5 M1 q! _9 d/ d9 J) D5 A2.2.6 列表框1 @ }: L4 O7 p9 ]$ L8 h u
列表框(ListBox)是在一个文本框内提供多个选项供用户选择的控件,它比较类似于下拉列表,但是没有显示结果的文本框。到以后,我们会知道列表框实际上很少使用,大部分时候,我们都使用列表控件DropDownList来代替ListBox加文本框的情况,在这里对列表框的讨论,主要是为以后的应用学习一些简单的控件属性。, L6 l+ o3 m/ Y
列表框的属性SelectionMode,选择方式主要是决定控件是否允许多项选择。当其值为ListSelectionMode.Single时,表明只允许用户从列表框中选择一个选项;当值为List.SelectionMode.Multi时,用户可以用Ctrl键或者是Shift键结合鼠标,从列表框中选择多个选项。. t w% f+ I! T- i; _
属性DataSource,说明数据的来源,可以为数组、列表、数据表。" K" H( i: O1 s( m1 H
方法DataBind,把来自数据源的数据载入列表框的items集合。/ _ X" n" _% x% p2 P* Z
例子:在这里我们将结合前面学习的按钮控件(Button)、复选控件(CheckBox)、单选控件(RadioButton)以及列表框(ListBox)给出一个实例。
- N1 L$ r% S8 J1 U% o首先,页面加载时,我们产生一个数组Values,并添加4个关于水果的测试数据到Valuse数组。然后列表框从数组取得数据加载进自己的items集合。然后根据复选控件chkBold的状态决定是否用黑体字输出列表框的选择结果。根据单选控件rbtnMulti和rbtnSingle的状态决定下一次列表框是否允许多选,最后按下按钮控件提交表单,显示选择的结果。
' v" c7 F6 K3 V' `; Q1.源程序(FormListBox.aspx)" m* R f3 L T
<!--源文件:form\ServerControl\formlistbox.aspx-->
+ T+ F6 Y& s" Z<html>( L, J# V0 q" b" \
<head>
1 i# ~- c/ J8 t% g+ P6 L. M7 F: Q <title>
8 {8 j$ t4 l; f* @( t ListBox控件试验0 m, d( M, v8 i- w8 F6 F! v7 l
</title>5 X8 n+ l T/ _, M$ I8 f
</head>
N7 F1 l/ z; U! K5 j G <script language="VB" runat=server>$ P, b. u* @$ ^% J8 Z# u7 d$ V
Sub Page_Load()
" S& Z% }4 k: L '如果选中黑体复选控件,把文本标签的字体设为黑体
9 j0 F X* ]$ S+ ^& A- V If chkBold.Checked
: X7 L! y0 R7 a7 b" [ lblTxt.font.bold=True8 H" D h2 c8 d- _
Else$ X7 j/ S/ x3 |) _2 h
lblTxt.font.bold=False
% z% f) q8 I2 w2 |; H' _ End If
' b2 C2 F$ w! O8 V/ V( X9 `8 e; f$ W1 z '如果选中多选的单选控件,那么则把列表框设为允许多选8 H& \8 p( N6 C3 T8 C# l
If rbtnMulti.Checked$ {. ]& W0 _% j
list1.SelectionMode=ListSelectionMode.Multiple8 ]' v2 | @$ n0 l3 J; n9 _
Else
3 q$ L# `7 v$ D% {+ l- O$ u! Mlist1.SelectionMode=ListSelectionMode.Single6 ~ A" M5 P' N1 m1 E6 R7 V
End If
# H- Q* w% S1 i If Not IsPostBack s) V( c9 H- d2 p; ]0 k
'第一次请求时,为列表框设置数据$ D4 o b# D) y9 {
Dim values as ArrayList=new ArrayList()5 r2 O' |( ^6 @9 Q9 s
5 X# F- \( [' O, c" D* [" R5 o
values.add("苹果")
9 l# }3 d) I) d! k5 t+ T/ X values.add("梨子")
0 _- R3 S$ ?( J+ V* ]/ x9 I( w values.add("香蕉")
+ i T4 z+ ^( @/ x' t values.add("西瓜")2 e# [/ L: i8 i! D3 A+ E# f
list1.datasource=values. j g0 L2 N' O6 w9 f
list1.databind
$ A# i; r3 J4 X9 O& f ^ Else
7 M8 S* ]" x$ G' H0 p/ T '把从列表框选中的内容赋予文本标识,如果未选择,显示"未选择" ( S# z9 V1 a5 ?% [
Dim i as int32
8 _8 V0 ^" f& Q% f! M/ [ Dim tmpStr as String
+ t, I9 U+ |( H$ S7 B$ L1 ? ‘对列表框list1的items集合轮询,根据其Selected属性,判断是否被选中
( K9 K7 J7 o2 Q2 s7 s- WFor i=0 to list1.items.count-17 ~0 W3 h; }: Q: Y; }
If list1.items(i).selected % V- H: I7 x& j, m; J3 [
tmpStr=tmpStr & " " & list1.items(i).text; r! f8 `0 W2 j
End If- l) Y1 K0 E" s* M* Z
Next$ v' N, `& |$ U% |3 F# Y }
If tmpStr is Nothing
8 Z3 I$ @& ?7 v5 X! }, h5 @* r1 M tmpStr="未选择"- U2 R% G# l' |3 Q6 k6 @, c
End If# r8 a( S% B* q' Y' j
lblTxt.text="您选中的项为: " & tmpStr( c+ M( W" Q( d+ z
End If
! f( s% T+ k) OEnd Sub* [0 A7 t, a! w& ]' C
</script>
, O' l2 d+ t* H @6 l <body >
' [! t; k% Q6 |: R6 _ <center>/ t6 ` c! b7 i9 O
<h2>ListBox控件试验</h2>) l/ h& ^- S1 {; T* t; \# R
<hr>- F7 I4 h' w' a/ q4 q9 G6 P# C0 V
<form method="POST" runat=server>
6 s, k, e l" b' T) F0 V请选择水果
* B; A. v5 V$ A. f<br>! M( J9 K) ^) k
<asp:ListBox id="list1" runat=server/>
: N) ?0 D4 |/ J; f* e. R<br># \4 r2 J4 {3 { W
<asp:CheckBox id="chkBold" text="黑体" runat=server />+ [, v/ y8 A. o
<br>
5 I) v9 ^/ `* W* d: a8 {: Q p ‘第一次设置为单项选择3 y- p# C N5 G% D& S) h j
<asp:RadioButton id="rbtnSingle" Checked=True text="单项选择" 5 D# J/ f) b7 Y6 N; u
groupname="group1" runat=server />$ t8 Q0 p3 K" S% d& g
<asp:RadioButton id="rbtnMulti" text="多项选择"
E* @- ?! |& R* H+ agroupname="group1" runat=server />; Y& C! h7 K6 X+ o! K
<br>6 g! E$ K3 [' Z: U R$ l
<asp:button text="提交" runat=server />/ N. O3 G& }- n: T! q) h5 J
<hr>
% K% X! |; E# z3 K6 M- D# Z/ c# f<asp:label id="lblTxt" runat=server />
4 Z( [. y" r2 ?7 E8 W5 m5 k </form>' z; ?2 f$ B! F. |
</center>
7 M \+ N# y d+ a8 J+ j& m </body>4 O2 a& g3 ^- h0 b3 \( t5 t- Z/ W
</html>
, E$ L+ W& U3 K0 `. B P2.开始时的画面输出,第一次缺省设置为单项选择: Z9 F* ~% t. w$ ?' q
{% Q" _. G3 Y/ n
3.选择以黑体字输出,并且允许多项选择后的画面:+ `0 r; F: S( F+ g3 K9 w% I
# m9 I9 A! p+ z
, S" m! E3 I9 Y. @+ x, g' S$ d9 O$ q2.2.7 RequiredFieldValidator
" L5 F' S V$ g/ ?: x1 z这个RequiredFieldValidator服务器控件保证用户不会跳过一个入口,如果用户输入的值跟符合RequiredFieldValidator的要求,这个值就是有效的;否则,不会跳过这一输入步骤而往下走。/ p5 `" ~% l. `0 ?
下面的例子(RequiredFieldValidator.aspx)验证了这个要求:' M( e5 ^& ?. a* \' u1 r
<!--源文件:form\ServerControl\requiredfieldvalidator.aspx-->6 S- ^2 r5 T3 G5 \9 B6 M
<html>: h$ j$ A9 r7 k* a
<title>检验</title>$ s# {1 F1 m/ l) N/ _3 E
<h3><font face="Verdana">.NET->RequiredFieldValidator Example</font></h3>
7 G: \5 }* S; W <form runat=server>
+ j5 u. t7 Y3 U& U Name: <asp:TextBox id=Text1 runat="server"/>
# X$ v8 ?/ Q# I9 n; _ <asp:RequiredFieldValidator id="RequiredFieldValidator1" ControlToValidate="Text1" 8 `2 U- v: K h
Font-Name="Arial" Font-Size="11" runat="server">
* @: y* J8 `4 q; [+ ]$ I Required field!
1 [1 p0 s; e' ?/ [4 ?, e </asp:RequiredFieldValidator>
' | {- p# j: e- M& U) Z <p>
7 Z( R2 x# d3 g4 n$ D {0 v <asp:Button id="Button1" runat="server" Text="验证" /># F5 C& f* w% @4 E
</form>- m! \( p9 V3 n* M+ P0 a
</body>
5 u. a5 W1 t# M; D/ f</html># D- E+ L$ ?7 {. s- N# g* e+ _" R& }# l
运行效果如下:7 s, R2 m- i$ O& ^+ t6 ]8 p
2.2.8 ValidationSummary8 U1 a3 X# i: K
用户的输入有时候是按照一定的顺序来的,有效性控件验证用户的输入并设置一个属性来线使用户的输入是否通过了验证。当所用得验证项都被处理之后,页面的IsValid属性就被设置,当有其中的一个验证没有通过时,整个页面将会不被通过验证。
; l4 ~6 Y2 A% e3 L$ M$ L当页面的IsValid属性为false时,ValidationSummary属性将会表现出来。他获得页面上的每个确认控件并对每个错误提出修改信息。
8 E. b8 j7 k: @( J; i& c% A文件Summary.aspx的内容:* Z) @; q+ J1 V6 X9 h- E' v
<!--源文件:form\ServerControl\summary.aspx-->
. q: m' g& |7 O( k/ _& s<%@ Page clienttarget=downlevel %>
) b0 C9 A2 q- h7 i<html>4 }; f' l6 b c% @2 h
<head>
6 \3 F1 t4 B" e$ r a! M <script language="VB" runat="server">
3 Q5 e! Q4 C9 W5 ~6 [2 f; } Sub ListFormat_SelectedIndexChanged(sender As Object, e As EventArgs)
$ s; Q% _, e1 M ' Change display mode of the validator summary when a new option- D! I% H% ?! S P8 D
' is selected from the "ListFormat" dropdownlist6 b: U/ H! S9 V2 |
valSum.DisplayMode = ListFormat.SelectedIndex
8 m) J1 z3 R. ?0 i% A
- ?1 @$ N& \/ M+ Z End Sub/ y! z4 T& q, C4 U9 w
</script>: B V0 o! R* P5 [4 |7 H4 C9 b- d
</head>
b4 E, a2 W1 @, h7 l<BODY>
, [2 }" e% h6 A& z<h3><font face="Verdana">ValidationSummary Sample</font></h3>
3 H* w. W! l8 H<p>6 x F# l$ O0 s" Q. S
<form runat="server">! K1 ~) n2 V; \
<table cellpadding=10>5 y: A& q% S5 ~! f Q
<tr># j4 A8 K2 _' o& g2 q& a6 t
<td>* n( d @5 |8 O! o8 h3 j
<table bgcolor="#eeeeee" cellpadding=10>
r& e0 k2 z2 G <tr>
( `3 R6 w! i ^# Q- ~0 J9 p <td colspan=3>" s2 `4 @& ?) `2 Q
<font face=Verdana size=2><b>Credit Card Information</b></font>
& J! w9 o( i: U- a9 [9 l </td>
& l" _/ C: ]: C0 `2 W' j |. }* c5 D </tr>
9 T9 F9 D- c V3 O <tr>
5 j2 D% O( J( W7 \* F <td align=right>+ B; e9 C( t( Y. [. |4 E6 C& f- k
<font face=Verdana size=2>Card Type:</font>6 h% k- O5 P7 p0 S, h
</td>0 r# f) q5 L+ q
<td>
' v3 K& D' G+ V! Y9 h8 c <ASP:RadioButtonList id=RadioButtonList1 RepeatLayout="Flow" 5 z" t! l- k. y
runat=server>' p: y, f- y4 n: w9 \( @8 v q
<asp:ListItem>MasterCard</asp:ListItem>7 }: x: n! t4 X* {, }$ R
<asp:ListItem>Visa</asp:ListItem>% L2 U1 o0 y. I& t6 g
</ASP:RadioButtonList>
% U) X" O, R0 [' W l6 ^( K- z7 d- [ </td>7 c# O) u+ U- e$ C8 {: V9 k! U7 U. b5 `
<td align=middle rowspan=1>
# r& }" Z1 ?/ n0 t# ]& N. d" X <asp:RequiredFieldValidator id="RequiredFieldValidator1"; `) c. p' h) L& s- `
ControlToValidate="RadioButtonList1"
0 \! f" Z9 A" N; m ErrorMessage="Card Type. "( W* o+ W3 Z4 N- A# z( W3 h/ z% L
Display="Static"; ~/ v; c8 K4 p% e
InitialValue="" Width="100%" runat=server>1 d0 @/ O8 D( z
*7 f) |4 p/ P7 B4 ]7 v- N1 |
</asp:RequiredFieldValidator>% A3 F% A, e/ p1 Z. c/ C
</td>8 t! G* _/ P( ~& E" }6 J3 [
</tr>( X1 w: n# a" g$ ^" h7 @
<tr>
: _# x: r( ~( x3 x <td align=right>5 V9 [' i0 f0 @. B/ @$ ^2 n
<font face=Verdana size=2>Card Number:</font>
F i6 |! J' M4 C- _: }& H O& { </td>
5 x1 Q- e2 N# Y9 k9 S <td>
8 E- K6 D/ }0 l+ v* e% F0 W7 o <ASP:TextBox id=TextBox1 runat=server />) b' ]0 v1 j2 j2 U' X0 Y
</td>, A4 e+ G/ E; C2 N0 o
<td>
7 R* G" A: f, ?8 P <asp:RequiredFieldValidator id="RequiredFieldValidator2"
% _4 v- e+ l0 \, m1 S8 Q ControlToValidate="TextBox1"
$ R$ K8 p: f1 q+ D6 D ErrorMessage="Card Number. "
1 g8 p3 \; \$ ^$ m+ G Display="Static"
6 Q0 M7 A6 b# t Width="100%" runat=server>; p3 w8 g0 b7 k1 l. Z4 v
*4 @2 A' m4 [- ~1 @# f
</asp:RequiredFieldValidator>
/ m, z8 W! S2 c7 a </td>+ E2 {( d$ G1 ^7 x
</tr>, e% G- r/ N, u+ Q" Y: J: M* d
<tr>8 |1 @0 K, n3 M5 n( H, Q
<td align=right>5 Z7 T+ m2 _! ?$ Y# d( ]
<font face=Verdana size=2>Expiration Date:</font>3 x+ Y) C/ n3 c, ^
</td>, {1 `4 ]: w+ r' a& G
<td>( f5 Q- b$ y) Q/ W0 O
<ASP:DropDownList id=DropDownList1 runat=server>
" J: G4 i! p0 x& m2 m, c* i <asp:ListItem></asp:ListItem>/ y3 F5 p7 y) R$ M- ^; F2 }
<asp:ListItem >06/00</asp:ListItem>
5 ~0 A) e6 y* W <asp:ListItem >07/00</asp:ListItem>( G$ b2 H8 t- f
<asp:ListItem >08/00</asp:ListItem>
+ Z$ f0 j" y) t) }9 a <asp:ListItem >09/00</asp:ListItem>5 B, ]8 r/ W1 N$ Z2 u6 S8 D
<asp:ListItem >10/00</asp:ListItem>
7 {; ^6 B0 ]3 n/ }+ ^9 f% g2 t <asp:ListItem >11/00</asp:ListItem>
% \# o+ M, S9 P9 m4 w <asp:ListItem >01/01</asp:ListItem>0 D; p9 I. k# l& g
<asp:ListItem >02/01</asp:ListItem>% n' o+ i7 e& d& k. A
<asp:ListItem >03/01</asp:ListItem>
3 W& A- W+ Q1 y' ~; f: g! W <asp:ListItem >04/01</asp:ListItem>( Y& J- d% E# X9 `4 y! \
<asp:ListItem >05/01</asp:ListItem>
6 G" s9 f2 ^' X. f3 D <asp:ListItem >06/01</asp:ListItem>; J6 s7 n( m$ I& `7 |; P
<asp:ListItem >07/01</asp:ListItem>0 S! u$ x( Q4 H8 z3 l: N3 V- v2 p
<asp:ListItem >08/01</asp:ListItem>* ` U7 b! Z- ~4 E
<asp:ListItem >09/01</asp:ListItem>+ t4 m0 s; y. v6 a% ] {2 `, h! P
<asp:ListItem >10/01</asp:ListItem>
9 W7 o( }* M1 P* c: ~7 D1 r8 ^ <asp:ListItem >11/01</asp:ListItem>0 r( Q3 V- c& c0 e! \( K
<asp:ListItem >12/01</asp:ListItem>7 J s: W8 x! G& l/ R* u6 H; q9 h! h
</ASP:DropDownList>
+ j- z+ }9 r7 l, ]$ c( r" |: v2 t </td>
( f6 u+ i4 ]1 T2 k* O) \ <td>
6 m2 y" N9 c6 i' T, j! A <asp:RequiredFieldValidator id="RequiredFieldValidator3"; I6 R& U! M6 Z j- x" q$ ?
ControlToValidate="DropDownList1" % R' e6 ~7 O4 S( u: p
ErrorMessage="Expiration Date. "
, P8 i4 Z Y; y1 k& Y8 \ Display="Static"5 l! r0 s8 [* y- f: i) M/ n/ X
InitialValue="" & ~* P! A. E) ` J0 i! M, m3 v4 K5 S
Width="100%" 6 \- m# J$ U. Q$ j" _, s
runat=server>; S, x* @6 g* a' z/ `3 r3 ^4 A& R( n
*6 D& f; `9 P- o- p y; U
</asp:RequiredFieldValidator>
: t/ ?1 }! q2 p </td>3 A: J% W4 u8 r' ]8 m _$ y! v
<td>
. Z2 m* S8 r9 V) m- e/ C2 ~ </tr>, s7 f" g1 r* Q" @& @8 D
<tr>4 L$ @' R9 N0 K2 q6 Y1 e3 r2 o* H
<td></td>8 g: f P4 h# \+ {6 ^
<td>
" g4 P3 A! j4 k& I5 R2 R) Z4 [ u% ~; N% n <ASP:Button id=Button1 text="有效性验证" runat=server />) ^& O% ^: k) H4 O p' Q/ G$ J3 b
</td>
0 R; _8 p5 Z" z: a <td></td>
3 k, M- C. g" z5 p* w# B) Y/ C </tr> t1 u1 m) q/ V/ t+ q9 H: g) ~( @
</table>
& p+ W, y" P, \ </td>( j3 T. A/ ^3 F. I+ M
<td valign=top>
8 q5 P. n3 g( ] <table cellpadding=20><tr><td>( D# A, g" G4 ?& ~9 d
<asp:ValidationSummary ID="valSum" runat="server") v9 f3 y) h U( E, U$ g. W* x
HeaderText="You must enter a value in the following fields:"7 d/ H: N& R$ B$ q. {- V, O: L
Font-Name="verdana" 0 m5 ^ B- F( n, X; k
Font-Size="12" 6 Z3 Z. R1 C$ k; _. p8 x
/>2 g6 u ] e* I/ o! j6 V
</td></tr></table>
4 ~3 `8 t! C1 m </td>" o* G3 K- j" }/ g8 d
</tr>' j, J @5 W% Y- [, [, X$ X& F$ S
</table>
1 D; i. {+ u, ?0 Z0 \# b<font face="verdana" size="-1">Select the type of validation summary display you wish: </font>
o: `$ | n1 w/ _3 d( i<asp:DropDownList id="ListFormat" AutoPostBack=true
' Q9 f) \6 L& M6 X9 z! u6 tOnSelectedIndexChanged="ListFormat_SelectedIndexChanged" runat=server >
7 w8 F0 t6 \' D* x( R0 C4 P <asp:ListItem>List</asp:ListItem>
* _3 p T3 Q/ ]/ t, \" K" I4 {7 t$ v <asp:ListItem selected>Bulleted List</asp:ListItem>
: z5 ~1 h* p+ e- U0 F/ _0 C <asp:ListItem>Single Paragraph</asp:ListItem>+ F+ H, P% O' ]+ p- `3 ^
</asp:DropDownList>
- B5 ?& T3 t0 ]+ i6 s</form>
& q. R; U i- x- g3 B</body>
0 C4 j9 i$ j' a) s) E</html># Q' M4 ~/ L7 |
结果如下:
- l x5 ?; H5 ?0 ]8 | H2.2.9 使用panel控件
! P' A8 f* `& o0 S! p) a 我们在进行用户信息录入的时候,如果使用ASP程序写的话,通常需要三个网页: o) ^" y: g- }
① 进行用户身份检查
- ^1 F2 Z8 P" j ② 填写相关的内容
4 b6 S+ } ]1 n3 p* _7 u3 e* U ③ 显示你填写的内容$ u) s- |: ^0 U: b( r6 M- T. {
填写的流程如下:
4 ^# \7 i+ n2 ]0 k- S9 C$ O
# K9 L% o% {* y
% j$ n8 a: }7 b! r, c) B这样的话,我们将分别设计3个网页。这样会显得很麻烦。可喜的是,我们可以使用asp.net0 N* \( P9 n- _1 u% A" [* [
中的panel控件,在一个页面中即可实现上述的功能。
8 G* _! A6 p* M; X4 d5 Q1 v) F流程如下:1 O1 Y. O# d% V" \+ u( [4 {
A- m" p; o% l8 A) o6 t
- A0 Q. r4 N d6 K' B: D% }/ T6 H- j# X# E! T3 @+ b* F/ ]' H
6 P# Z& y+ U; T- J, Z. J
$ ~4 `# _- K# s- S; U N* V
) b: z( N3 d* W' X1 i* }
0 H |, b8 `' r5 n5 t9 h# p/ x g
# @+ C" L8 y" I. y好了,有了上面的叙述,请看panel.aspx文件内容:
' a1 t6 m8 |6 `4 V& q<!--源文件:form\ServerControl\panel.aspx-->2 i( [" Z$ d. g8 h6 }! |) O
<Html>
: ^( G' Q5 @+ i7 @( x <Body bgcolor="White">0 ?. @3 x2 e3 O' @6 m+ \+ [# M
<center><H3>使用Panel控件示例<Hr></H3></center>
; M* _( L) E0 S7 i7 W<title>使用Panel控件示例</title>
. g* H0 C* z. `; V- d9 Y( t<script Language="VB" runat="server">
6 E# F" m& V L" f/ W Sub Page_Load(sender As Object, e As EventArgs)
8 \, b. g1 P- n4 h7 m7 l0 N If Not Page.IsPostBack Then V3 Q, h) O6 I! Z9 `
panel2.Visible = False$ |1 Z0 ^/ l6 e% A$ G# G
panel3.Visible = False4 D4 o# q2 s5 |$ l9 J
End If- ~/ ^4 x, F8 C( q# V) Q$ }! T6 y) U
End Sub
2 U6 Z; Q2 A9 d9 |3 C/ \Sub Button1_Click(sender As Object, e As EventArgs)
0 g1 t! v' Q6 ^/ X panel1.Visible = False
) N0 G0 V+ Y/ @% p& W, B panel2.Visible = True
' S% i3 ?! q) z End Sub
0 x+ N; C4 I8 e* Q: B- r/ d
% h+ V& W8 T2 k5 f Sub Button2_Click(sender As Object, e As EventArgs) R( t0 b9 @2 `" T: ?% E7 E
panel2.Visible = False; D1 E) T8 N( @/ A3 o1 L
panel3.Visible = True% r: p; X* z* o8 S7 L- @7 }2 O
Span1.InnerHtml = "用户名: " & UserID.Text & "<BR>"8 [5 A- W! Z( j m+ M5 G9 _" e) P' F, J
Span1.InnerHtml &= "密码: " & Password.Text & "<BR>"
2 r5 i& X8 l* Q Span1.InnerHtml &= "姓名: " & Name.Text & "<BR>"
8 K% T0 U8 a' _; I5 q Span1.InnerHtml &= "电话: " & Tel.Text & "<BR>"' L- ]4 I3 P3 G5 z
Span1.InnerHtml &= "E-mail: " & mail.Text & "<BR>"
* g( _/ w- ?( l y2 b Span1.InnerHtml &= "地址: " & Addr.Text & "<P>"( U1 ?& y# B s3 y6 \6 g+ G1 b- S7 ]
( q( t* r( H/ H) v' ^ End Sub; c( G) |2 ?5 a$ D3 c7 _, G% u
Sub Button3_Click(sender As Object, e As EventArgs) , E0 Z. x. U1 }+ S- R2 f
Span1.InnerHtml &= "输入完成!"' ~! l8 w) M4 Y9 A
Button3.Visible = False2 v3 [4 ^3 z5 d: ~ U
End Sub
: j* J$ [+ R0 Z a8 h </script># I* X @3 T1 v
<Form runat="server">
2 R# }5 v, j; w<center>
, ~: S- P, [) q! q* `- s; P) F& l<asp:Panel id="panel1" runat="server">0 A7 {% n6 M2 Q( m+ y/ h& h. R; M
<Font Color="#800000"><B>第一步:请输入用户名和密码</B></Font><Blockquote>4 B1 ^3 s6 Z9 u! T( c
用户名:<asp:TextBox id="UserID" runat="server" Text="kjwang"/><p>
9 B5 J: Q; @ K& u0 r; `! E- S* @ 密码:<asp:TextBox id="Password" TextMode="Password" 9 e5 x0 B% b- n; K
Text="kj6688" runat="server"/><p>
. {1 |! I8 M' O1 N( H <Input Type="Button" id="Button1" value=" 登录 "
# ~3 u. ^+ x s) c" i+ M1 F4 q OnServerClick="Button1_Click" runat="server">
# O1 G3 H' j; |: a </Blockquote>" s/ O8 N3 S7 S0 E) B6 m# Z5 i- P
</asp:Panel>
5 @! g; \# b, ?8 e* @- R8 a <asp:Panel id="panel2" runat="server">
6 ^7 }/ t# T+ G& x <Font Color="#800000"><B>第二步:请输入用户信息</B></Font><Blockquote>$ ]2 R! v* y) U+ z; W6 A3 K3 G
姓名:<asp:TextBox id="Name" runat="server" Text="小李"/><p>
3 _2 f3 \3 U3 F* a 电话:<asp:TextBox id="Tel" runat="server" Text="(023)65355678" /><p>
. A) H' P2 S' o- j E-mail:<asp:TextBox id="mail" runat="server" Text="jimmy.zh@263.net" /><p>
1 b1 t1 A% b+ Z3 H 地址:<asp:TextBox id="Addr" runat="server" Text="重庆市人民路115#" Size="40" /><p>; @+ g# p5 R8 O
<Input Type="Button" id="Button2" value="申请" , d0 v: {+ P8 O2 z- ?9 Z* e, k
OnServerClick="Button2_Click" runat="server">
( F; @ w3 q; k( Y3 s' d" v </Blockquote>
G( N; X- |: o5 @7 o6 U </asp:Panel>
3 i5 g! o: e/ e, i" {- t( N <asp:Panel id="panel3" runat="server">) p% ^( c, W# s$ h% q/ R
<Font Color="#800000"><B>第三步:请确认你的输入</B></Font><Blockquote>% J% h7 t* }. m) @" M5 W; a
<Span id="Span1" runat="server"/> c- L% c, ]7 j0 ^2 q( P6 f: h$ n% V
<Input Type="Button" id="Button3" value=" 确认 " ) t% e6 `7 W/ k {
OnServerClick="Button3_Click" runat="server">
7 B( A4 y! ^" F+ _% I& K </Blockquote>
% b: m u" `- c) a- Q& z* D$ ~ </asp:Panel>( X0 @& [; u! Z& D
</center>
! V; ?2 L: m- x8 E! ], P+ z </form>
& y# X+ q6 C- _ <Hr></body>
7 @! e3 X' Z$ }' ^* E+ {, k </html>
0 y# A8 {4 i* W V请看程序的运行效果:
9 a. Z1 z+ W$ g f
$ h- X4 U! K, `+ ~- {
: O0 L' r8 @+ X e6 a% F* q, U8 u& H2 X( o
7 b' ^9 O- B9 y, F" `
第一步:$ V! v7 a; j8 R; Y' c
) D6 A* [& R! K
第二步:: A# z0 k; t# A8 `( h
- H* l, o! A6 z: O
第三步:
3 R* l$ U& y: K7 X! Y2 A
0 ^+ |4 F$ [9 a, G大家可以留意浏览器的地址栏,我们注意到地址都是相同的。我就是我们使用pann控件所得到的效果。$ `( L) S: u! u: Q+ K) Z
2.2.10 选择控件
; [, R1 a( O! t选择的方式有两种:单选、多选。我们下面用具体的例子来说明这两种选择控件在asp.net, `2 O' i0 I- Y% A2 l ^3 H4 m
上的实现。
' W- |# _2 ?+ _% ]. i对单选控件,asp.net里面有一个专用的表示:RadioButtonList,我们看下面的代码:+ }. p% ^4 e# c) t3 T2 w8 {
<!--列出选择内容-->
3 m$ e4 ]5 B; N& O/ i<ASP:RadioButtonList id=ccType Font-Name="Arial" RepeatLayout="Flow" , C7 M2 u+ u; ]* c! N9 y
runat=server>2 G0 ]" b0 [+ h; f2 M8 X" {5 p
<asp:ListItem>招行一卡通</asp:ListItem>
" O" x6 ]9 F8 E) k, c2 F <asp:ListItem>牡丹卡</asp:ListItem>4 |4 y% z8 A1 d @
</ASP:RadioButtonList>
# t7 D" G7 j5 L* r我们在取值的时候,就可以用一个语句:
+ M! T2 `4 R/ v. e0 z( B( u+ qRequest.QueryString(“ccType”)
3 j( w I2 F8 ~. ^) W* E" F& Y来取得这个值。下面是我们这个说明的完整代码(RadioButtonList.aspx):/ x7 A. |; T" V- x6 Q! z
<!--源文件:form\ServerControl\RadioButtonList.aspx-->
- k( K* T8 u* S) g5 d<html>
9 I: {( ^8 Q$ b<body >6 [6 D" O; s# n! J2 v
<center>- k& ~* O5 C: c
<br><br>
4 p1 E) D) S3 C; l- o7 B <h3><font face="Verdana">.NET->选择控件!</font></h3>4 t1 T# g. m, M! s7 H- M x
<br><br><br>' ~' y/ K0 ?4 Y. f
</center>" M# q$ \0 U! ]8 T+ a" z+ O
<form method=post runat=server>% I# V0 c2 X$ r
<hr width=600 size=1 noshade>
/ ~# K+ Q0 U! \+ L0 f; k <center>
# s7 Q6 n- }; y. d6 h; U) D <asp:ValidationSummary ID="valSum" runat="server" 8 |% G X/ Q3 {, D" R. X ^5 J
HeaderText="你必须填写下面的内容:"
+ S% g3 k+ C% W9 b DisplayMode="SingleParagraph"
# | O- v; d7 i7 V2 D Font-Name="verdana"
" l8 K' H' e" G1 X, `# \ Font-Size="12" ( }1 g+ T; B: f8 p G4 F
/>. ^# _ h* }7 ~8 u! V
<p>
! P+ h' _) i/ p& r <!-- 信用卡信息 -->
5 o# L2 u7 y6 x! j <table border=0 width=600>
% l- N& J9 u; l- F3 r <tr>8 N# f: Y! [- `; @
<td colspan=3>
5 v1 x- C$ q+ V7 k9 F d) Q/ @5 X <center>+ H3 H' [" ?- k& V
<font face=Arial size=2><b>信用卡信息</b></font>6 m/ }8 {7 D$ |8 B5 Q- u; d+ E2 y
</center>1 C7 b9 o. D0 P2 ]6 I4 s" {% J
</td>
) R9 Y9 g( Y4 ]5 ~; H </tr>" M# g7 a; H" @- I
<tr>
# v3 }: E1 r+ b) Y <td align=right>/ u5 B* Z# |7 [6 {( B
<font face=Arial size=2>信用卡类型:</font>. @0 o/ j7 v; H* o
</td># B1 _# S( p: _: v
<td>
7 g @+ l! F' l& o) f<!--列出选择内容-->
! E% R" R) G# k7 O <ASP:RadioButtonList id=ccType Font-Name="Arial" RepeatLayout="Flow" runat=server>
: [6 r+ c% a2 j1 D0 D <asp:ListItem>招行一卡通</asp:ListItem>
! o+ r& [" e. ]& J0 a <asp:ListItem>牡丹卡</asp:ListItem>5 b/ P* ~5 y9 I3 d) A. Z
</ASP:RadioButtonList>$ d8 j# O# T, d: z
</td># W0 M' F4 G& l, A: u+ h4 d
<td>$ H5 v( T) u8 g/ U/ T) g6 J
<asp:RequiredFieldValidator id="ccTypeReqVal"* E) w4 \& R1 w
ControlToValidate="ccType"
( M8 R6 i* e# g ErrorMessage="信用卡类型. "
& [9 c4 [( J$ y+ {" H$ D Display="Static"
/ U4 @2 S4 T& d1 U, r, a InitialValue=""+ ~6 y3 F' f7 k% U2 ~1 b+ G
Font-Name="Verdana" Font-Size="12"
( V8 J, S& I c' H- t runat=server>2 c, E$ k j3 Q9 O7 i* z
* f$ Z2 t7 {% C: O2 S
</asp:RequiredFieldValidator>
: m7 {6 g: x/ i I, `4 K4 r8 b </td>( M) W) s) Z y' A
</tr>/ k! ?: @. I: x; k+ k6 }$ D
</table>
* N( G; i. B: d <p>
% T h/ c& M5 y( E& ` <input runat="server" type=submit value="提交">
0 Z7 k; A4 l0 ?8 C <p>: e1 Y& m+ R: n6 H
<hr width=600 size=1 noshade>
3 O* F- p# N# U% C% i</form>/ }- h0 r O% e f* {, S
</center>
9 R& Y5 r- Q8 A5 E' y</body>& l) v# O+ f& \
</html>1 O' L( ~4 j2 D' |9 h/ q8 }+ |! L
我们的运行效果如下:
* [9 Y3 Q% c, p: r+ J- z" T3 g( c3 O
' |/ s; l' o5 C$ h x+ `* O9 s
. S/ K) h* _* U9 `我们选择一个并提交,则会提交成功;反之,如果我们没有选择就提交,会出现如下的信息:
6 i/ F/ F# E% C0 V3 l( |( R) C) U0 q- E7 {) u% z) X
我们再来看看多选的情况:+ [4 D! S; N* C& s7 K9 x- I
'选择项列表
$ [% a' o' `! C1 e. n9 ` <asp:CheckBoxList id=Check1 runat="server">$ ?( t% Q8 i! b" N
<asp:ListItem>北京</asp:ListItem>5 R) }! Y. {# z7 e# W+ K& L1 y: I2 i
<asp:ListItem>深圳</asp:ListItem>
8 h3 Y* i: A# ~ <asp:ListItem>上海</asp:ListItem> J8 ]0 X! ?4 k% a7 Q/ s* ~2 G0 g
<asp:ListItem>广州</asp:ListItem>( _) i2 Z! r7 Y
<asp:ListItem>南宁</asp:ListItem>, d$ l2 e- n5 K0 {$ N4 W& ]
<asp:ListItem>重庆</asp:ListItem>0 m4 Y2 e9 ^. ~. C% A s
</asp:CheckBoxList>
# f- |( a3 A2 D3 q跟我们上面的单选控件就相差在定义上,我们用CheckBoxList来描述我们的选择框,我们写一个方法来响应我们的“提交“事件:
. c- q" S$ H1 m) L" ['响应按钮事件
+ ]5 [) I# r( r Sub Button1_Click(sender As Object, e As EventArgs)
$ I1 o* p' D& H, ]& v4 T) i( k9 ~* o Dim s As String = "被选择的选项是:<br>"
: E# {, D5 M" Q7 i/ P8 R, J Dim i As Int32
0 {5 }9 y0 J$ e! O3 [/ y For i = 0 to Check1.Items.Count-1; r' }6 N& Y3 M a0 f& I: \
If Check1.Items(i).Selected Then % @+ W# [+ |* n. m0 Q
'列出选择项
- D( ^( _' _( |/ I& X, Y( B& Z s = s & Check1.Items(i).Text
2 m3 U/ _% r# F6 a! l& O s = s & "<br>"* U; r- W% B" o1 |, z
End If
' L% V! x. ]% S: I$ l Next
0 P$ g2 X( Y8 d# F: d '打印出选择项
! q5 P8 u( |- a3 t4 i t8 L Label1.Text = s
1 @! D. c2 D6 c$ U6 l' KEnd Sub/ c" x5 h0 F6 w3 u; i k3 @# A% S
这个方法为定义打印被选择的选项。具体的内容如下(list.aspx):
: B! B! W0 k2 j2 p: L9 o<!--源文件:form\ServerControl\list.aspx-->' _/ \6 x$ d0 s+ B3 G
<html>
( g5 e1 g7 }% X% M<head>
( W/ \4 X# e% o/ B* q1 C <script language="VB" runat="server">
9 K. G! v8 w! p+ ^6 Y '响应按钮事件2 n1 s' ^$ H3 k# E- g! j9 n
Sub Button1_Click(sender As Object, e As EventArgs)
6 b7 l$ a7 P4 X3 [8 J* } Dim s As String = "被选择的选项是:<br>"5 G) F) y& l( x+ q9 w$ Z* V
Dim i As Int32
- K1 v; w7 V5 Z For i = 0 to Check1.Items.Count-1
8 p; z& {! f0 I2 s% |2 D. k# P5 F If Check1.Items(i).Selected Then
' _. E$ |, [% F+ l& K! l- k '列出选择项& M& a8 I y% I! {8 h: O
s = s & Check1.Items(i).Text
2 ^& z4 ?- Q9 M7 D3 n+ Q+ B" l s = s & "<br>"( T) C6 b9 \8 r4 |3 z. I
End If
5 ~ k3 M8 n6 w0 f# R; P Next
! G; n* a; j/ F" R+ S '打印出选择项( t+ X) N; e0 {; N5 J$ y" A* q
Label1.Text = s- }0 v8 |) R- t8 t* ^7 j
End Sub
% Y% v: D; i$ R% d8 P3 o: I </script>7 H) l6 R' W# e X2 ?
</head>+ _" x7 g. i6 g. m+ P$ B$ S( [
<body bgcolor="#ccccff">4 d- l+ i, U8 G" i: L
<br><br><br>5 j9 A6 w F* h1 F9 v2 d$ w; p
<center>% r _% c7 L/ D+ I" p
<h3><font face="Verdana">.NET->CheckBoxList</font></h3>- J+ x3 C% R1 L* L; j0 b% k3 b
</center>
* [( A. K+ N5 C; ?% l7 {0 V1 ?& f: L R<br><br>3 |, N( x1 _1 d3 d: Z) L
<center>1 E/ U4 S) p2 Q/ r
<form runat=server>
( e# B3 @ i) O5 f'选择象列表9 e o# m: W+ E) K7 m
<asp:CheckBoxList id=Check1 runat="server">% u- U, O. X7 z9 u+ B
<asp:ListItem>北京</asp:ListItem>( r4 N' B2 r1 z" _
<asp:ListItem>深圳</asp:ListItem>3 G/ J/ M8 M- A
<asp:ListItem>上海</asp:ListItem>
( q& b( }* O+ E0 ?0 d, U. \ <asp:ListItem>广州</asp:ListItem>
8 E8 M: j& l: ?; y" } <asp:ListItem>南宁</asp:ListItem>5 T D: M5 B0 P
<asp:ListItem>重庆</asp:ListItem>
! R. H5 u4 `7 u f/ K </asp:CheckBoxList>: ~( Z% {: [. A% }, Y6 I3 [
<p>0 j x# _7 x# n% _" U' X
<asp:Button id=Button1 Text="提交" onclick="Button1_Click" runat="server"/>
' ?8 E5 f! L5 b/ z5 @ <p>/ h$ L& w/ K6 D% N+ }* @
<asp:Label id=Label1 font-name="Verdana" font-size="8pt" runat="server"/>1 o; ` P9 P4 {; }, p
</form>6 {( w! ]+ {7 V# Q/ Y3 {
</center>: R' @" k7 g Z7 t }
</body>2 ^& \# W6 d. t: T1 f
</html>$ Y6 P% \# E6 Y" c
* q/ Y: [/ A, p4 _我们看到显示如下:
: C1 \ [$ y: z% Z: z% x1 L7 R1 E5 B
选择几个选项,并点击“提交“按钮“,看到如下的情况:7 `/ m& Y" q% |
' E6 G) U9 H) F/ m9 ?$ G) t+ ^$ ?- |3 a
# @2 ~' i, U% k: N
2.2.11 ImageButton 控件. {, F+ E7 @1 k2 U* H6 B
ImageButton 控件( G& K1 F( v. n% X0 t
当我们在浏览网页的时候,可能会发现这样一种情况:当鼠标移到图象按钮上和当鼠标移走的时候,将会发现同一按钮上将会显示不同的两个图片。我们可以用Image Button控件的 onmouseout 和 onmouseover 事件来实现。: c$ w- ?: H5 K: l
请看Image.aspx中的代码:* J$ `# H7 d9 O% q# I# ?
<!--源文件:form\ServerControl\image.aspx-->
. O4 n" w/ p7 J+ g- u<html>" Y- e# u* z. l- M0 i
<Body BgColor="White">& o5 R5 R( O; q0 K. B
<center><H3>ImageButton 控件演示</H3></center>
3 F4 n5 @6 b! ^+ q<title>ImageButton 控件演示</title>
! J7 y7 v/ ~: `% g<script Language="VB" runat="server">+ T6 C. c+ e% b- b( _+ o
Sub Button1_Click(sender As Object, e As ImageClickEventArgs)
( P" p) N4 [0 \! Q2 ^+ D; T’定义当我们点击按钮的时候,将访问的网页- W% M3 u/ ^; d0 ?8 v6 S5 D l
Page.Navigate( "http://www.yesky.com" )( H) q4 E3 W& F, J2 e
End Sub# ]2 E, O9 ]* b$ x/ b; c: k( [( N
</script>
9 w/ r, [3 U6 |* n) G0 U8 O<Form runat="server">
, l7 z6 k6 F7 s- K |2 D. K- \<center>
5 Y _+ y1 ?! H/ y6 l7 `& u+ _$ v<asp:ImageButton OnClick="Button1_Click"" `5 [; ~! W0 E$ F9 J3 Q
ImageUrl="18.gif" id="Button1" runat="server" ) N% `" h$ ?4 Y, A+ W4 `
OnMouseOut="this.src='18.gif';"
, B3 i; |) S0 R4 ZOnMouseOver="this.src='19.gif';" />
% g) K8 H9 N5 [$ x& e</center># U5 H _7 x. v( E5 j8 n
</Form>
% w, x- b% E0 k2 ] M/ b<asp:Label id="Label1" runat="server"/>% G9 I/ W: x* l! x$ j
</Body>
) x# X7 _2 m# W4 n& r</Html>5 R) m* O; v+ h) H( p! H" @
在这段程序中,我们使用了onmouseout和onmouseover事件。
3 d- @ U1 ^" m0 ]$ T. ^& Z请看程序的演示效果:* ^) D& z9 {7 Y+ r/ a
当鼠标移动到按钮上的时候,将显示:
; F9 H6 |/ ]% Z# }; y
% R4 d' \; k% z' m1 O
* [4 P; W3 U$ W1 j1 V2.2.12 列表控件- j! Y% j" w# X; e! t9 O' L
在asp.net中,有几种方法可以应用于列表控件。我们可以在aspx代码中直接嵌入相关
- t8 `, _: J8 k9 O' K: O, T的代码,也可以在页面装入的时候加载这些列表信息。8 L: K4 l; k c% D( e
下面是具体的应用,我们先看看在aspx中的列表方法:0 V( ?. s. L; E9 _' t6 [5 s6 i
<!--列表->列出内容-->* x* T( ~' b3 w. Z
<asp:DropDownList id=DropDown1 runat="server">* B4 M7 t. W9 v3 C
<asp:ListItem>北京</asp:ListItem>& x; N- U; G9 |3 O4 a( `& I9 V. V
<asp:ListItem>深圳</asp:ListItem>
8 j7 T; ?5 [" e6 ]; Z$ S- c7 b <asp:ListItem>上海</asp:ListItem>0 |9 i9 |1 w3 x4 u
<asp:ListItem>广州</asp:ListItem>
$ L2 ~! m7 U$ w7 m- G <asp:ListItem>南宁</asp:ListItem>
0 Y* g( N4 Q- I" Z1 { <asp:ListItem>重庆</asp:ListItem>, u, x+ F- h+ Z6 u$ b
</asp:DropDownList>
* W9 C' D5 e: U' o0 ?/ D9 d' A我们在需要取出所选的数据时,直接去取id值,即DropDown1;我们再定一一个方法,响应“提交“按钮的事件,就可以了。下面是完整的代码(DropDown.aspx):) n! W+ D9 g: C9 \% c$ {
<!--源文件:form\ServerControl\dropdown.aspx-->
$ Y; L0 j0 j4 T8 A% l( t<html>
& Q. @3 H2 d: k9 q4 ?<head>
3 E. e; O G( u: W- k2 g
* b- h d9 Q# x$ t2 s! O* d l# t <script language="VB" runat="server">7 D# Q- `. @: U& [8 ]; K" T0 @
: x4 s3 c' _; Q7 ]7 J3 M4 \
'在点击按钮时候响应
+ d& b* K8 K2 T( [ Sub list_Click(sender As Object, e As EventArgs)
2 `2 S: w& F! V! q* `' L0 H Label1.Text="你的选择是: " + DropDown1.SelectedItem.Text* k) N* \3 }+ K4 W! B
End Sub( J3 U" v1 m8 ^& b$ a
</script>
- m& G1 V& W$ {7 d1 Q" R6 E5 @( V& K</head>
! B7 m1 d# d9 y' l% U( m8 D4 R<body bgcolor="#ccccff">
$ {: j7 d2 o& s. J5 {<br><br><br>% p( @( q4 u" {0 w/ ~3 E
<center>7 K) u: {; V4 {
<h3><font face="Verdana">.NET->列表控件</font></h3>' }* P2 I% M& _" k" w* B
</center>' V+ P0 o- S+ D
<br><br>3 p: `2 ^3 Q0 ^) }6 j" V
<center>( z, A. k" q! R/ x/ b
<form runat=server>
q. o' Q9 E, A/ H$ c5 F& E<!--列表->列出内容-->
% `% |# |$ n+ s7 F2 [! B <asp:DropDownList id=DropDown1 runat="server">7 ~7 D& |3 x9 s# p1 \9 d
<asp:ListItem>北京</asp:ListItem>
% K6 i+ @+ ?. U4 m" B8 `) @6 x5 `) o <asp:ListItem>深圳</asp:ListItem>
# q/ \( P: C5 [+ ?' d. _ <asp:ListItem>上海</asp:ListItem>. z6 X* k% ~8 R N5 S0 l
<asp:ListItem>广州</asp:ListItem>
) w9 ?1 X; _' b/ u, j <asp:ListItem>南宁</asp:ListItem> W A4 Q8 t ]3 a9 T8 D- o
<asp:ListItem>重庆</asp:ListItem>
2 m" K% Y4 K$ }( u9 Q* i0 \ </asp:DropDownList>5 ?' S( B+ M3 z. b4 D: \! K
<asp:button text="提交" OnClick="list_Click" runat=server/>
8 D6 N6 q2 u# r7 W <p>6 a$ J4 p" ?1 I9 z" |$ r9 y
! S: t( |* J( i <asp:Label id=Label1 font-name="Verdana" font-size="10pt" runat="server">
' a L2 l' T, p* F/ I* Y
; a: ~/ c7 i$ n6 C( Q8 g </asp:Label>
( x2 R/ u. L, O' t. h6 p6 \3 H </form>
3 d* K& b4 U9 }8 f1 U</center>
: L* E2 m4 q2 \7 Z T</body>+ D+ Q0 B# D1 k7 V
</html>
+ Z7 l4 \9 W4 q. V' M9 Y. C1 h2 Q* H我们看看运行效果:2 k" h1 l# G* `( e! a$ z R" ]0 V& p
9 q G# }6 W4 S6 j$ i; k( i5 p5 S5 o
" j6 C! z0 g: L( Y' M: b9 P
点击提交按钮时候看到下面的效果:
: \5 T+ S; t0 \' g( T, P0 ]+ @$ w" f' H* X9 n" D5 h7 L+ ^
9 M2 v( V3 F) O) Z0 |0 a" A+ j6 ?) P r" s下面我们再来看看另外一个列表控件的使用,我们定义一个在页面装载的时候调用的方法:2 `* Q* E( Z1 g8 v8 z4 C9 @# w
'再页面装载的时候调用的方法: B' d! z' [% E
Sub Page_Load(sender As Object, e As EventArgs)
4 c' g- _; n3 w' d- v+ R5 m If Not IsPostBack Then0 c! m& s/ i2 u# M2 @* s3 V
Dim values as ArrayList= new ArrayList()
! B- r" R! ?# y: t values.Add ("北京")/ [% c4 I+ [; ~' n1 E6 L& @1 p6 x
values.Add ("深圳")6 F* C8 n; j" G2 A: k1 ~
values.Add ("上海")" s2 i5 ~# _$ j4 L' {# \+ e
values.Add ("广州")- R" r% d3 }* f* f% w! L
values.Add ("南宁")
* B! u3 N% e' C U values.Add ("重庆")* n, L- g/ P- k2 B1 ^& n$ Z. X
'设定DropDown1的数据源为values,即上面定义的信息' r$ U9 `3 _6 t6 A4 q# \2 F# _
DropDown1.DataSource = values
( G6 i$ g5 g, ?+ y3 w x3 h '数据的绑定" N, R8 J( K1 C! @5 O8 B/ E
DropDown1.DataBind
( p+ b5 _6 d% D End If
+ q# c0 [/ ^7 S* M b) C# { End Sub
3 e& |" R# {1 f O$ ^5 E我们在aspx代码中调用它:
( ~3 A" W: |0 Y! m! c3 }8 ^<!--列出列表信息--> 9 r3 @0 d4 J G0 ]1 `( W2 q5 n3 ]
<asp:DropDownList id="DropDown1" runat="server" />
- \% g% |, W, Y0 W就这样的一个简单的语句就可以了,下面是这个文件的完整的代码:
! D! ?8 @/ ?+ |: `; y9 h( Z<html>
* T( Y9 u$ ~3 Q5 b' H# A<head>! s5 D. W2 u. v" F0 H
<script language="VB" runat="server">
5 A9 o. a4 l- w, o, B* S'再页面装载的时候调用的方法:7 k3 q& Q& U) l9 X- l4 s6 _3 ]4 p" Z
Sub Page_Load(sender As Object, e As EventArgs)
: ^, Y: t; {9 B* K( \5 t If Not IsPostBack Then
: J x7 }8 W4 L7 u Dim values as ArrayList= new ArrayList()- p( X' g- J* j9 s' N& I& k- |
values.Add ("北京")5 v9 h1 d8 n: e" _
values.Add ("深圳")0 c1 x+ Q+ g( w1 x' _! |
values.Add ("上海")
5 v! S T7 Z9 y8 t) \% d! e values.Add ("广州")2 Z# \* ]; o# s+ U4 w& q9 H
values.Add ("南宁")
8 i8 d+ R E! R) L* }- @3 G6 J6 I values.Add ("重庆")# x4 }% b5 B7 N: I
'设定DropDown1的数据源为values,即上面定义的信息
' d8 ^6 m; `6 `- t% o2 ` DropDown1.DataSource = values. y8 D1 W* c6 r4 b! `0 `2 }
'数据的绑定: K2 m" n$ E4 p, k. x. P/ `+ f
DropDown1.DataBind* _8 N, ^9 u$ r* V; q" T9 `
End If5 |$ L7 U+ m7 H1 `4 C, O+ i
End Sub
1 n+ e G& w- m; V. P. I'提交按钮响应的方法2 B9 x" n* T& N
Sub select02_Click(sender As Object, e As EventArgs)
7 b# l2 h6 w5 H2 ] Label1.Text = "你的选择是: " + DropDown1.SelectedItem.Text
6 }; _1 C& J" @, b End Sub+ n/ \! S& O: X `- w8 f
</script>
2 E. Y5 X+ n" \% a- i3 T</head>
% h# f/ G7 Y( @7 K0 q<body BGCOLOR="#CCCCFF">9 B1 \! C% B- X
<br><br><br>5 b9 C# B* E: r+ X
<center>
: i2 w1 ]7 B' X- ]4 l: t <h3><font face="Verdana">.NET->列表控件</font></h3>
5 |6 l& F% b3 i1 ~. ^</center>2 c9 r7 [ Q' h; I- g q, s
<br><br>) `% M4 M! [* h6 ]* k
<center>: j5 F# Z9 Z4 u
<form runat=server>
& l, Y# r! z' l8 ]- t! H' a<!--列出列表信息-->
9 q' s. t+ ^7 ` <asp:DropDownList id="DropDown1" runat="server" />
( s$ c: h4 Z, s" ]* P) M <asp:button Text="提交" OnClick="select02_Click" runat=server/>
+ r1 s. F+ h7 A$ D: d% B2 x <p>/ l3 O. D: x, f
<asp:Label id=Label1 font-name="Verdana" font-size="10pt" runat="server" />
/ X- s( [2 N; Z6 c; E2 c7 f </form>9 F3 }3 p' m: P! d+ a" Q$ h
</center>
8 t1 D1 a, V' |7 m0 q3 m</body>
4 t4 a/ A+ F; B5 E</html>
4 Z3 W2 o g: e8 G0 z运行的结果跟上面的一样。
4 n7 i& e1 b* q$ `0 R+ ]2 W. ^4 S, ^2.2.13 重复列表Repeator" O) \, z( S1 F/ T) P, t
这种服务器控件会以给定的形式重复显示数据项目,故称之为重复列表。使用重复列表有两个要素,即数据的来源和数据的表现形式。数据来源的指定由控件的DataSource属性决定,并调用方法DataBind绑定到控件上。这里需要说明的是数据取出以后如何表现的问题,即如何布局。重复列表的数据布局是由给定的模板来决定的,由于重复列表没有缺省的模板,所以使用重复列表时至少要定义一个最基本的模板“ItemTemplate”。
8 l$ ?7 i. G/ F重复列表支持以下模板标识,所谓模板就是预先定义的一种表现形式,以后我们还会就这个问题专门讨论,这里就不在多说。
/ K* [2 p3 e& c b2 `1)ItemTemplate模板,数据项模板,必需的,它定义了数据项极其表现形式。
$ V* K4 M. ? W8 T3 E) L- {2)AlternatingItemTemplate模板,数据项交替模板,为了使相邻的数据项能够有所区别,可以定义交替模板,它使得相邻的数据项看起来明显不同,缺省情况下,它和ItemTemplate模板定义一致,即缺省下相邻数据项无表示区分。
4 i$ V( b {, O9 D% @3)SeparatorTemplate模板,分割符模板,定义数据项之间的分割符。
- e$ v: w, A ? H4)HeaderTemplate模板,报头定义模板,定义重复列表的表头表现形式。 s; `( g: X# W
5)FooterTemplate模板,表尾定义模板,定义重复列表的列表尾部的表现形式。
/ @+ Z: b" N" v p( U3 w切记,由于缺乏内置的预定义模板和风格,在使用重复列表时,请一定记住要使用HTML格式定义自己的模板。4 L$ u+ H$ O i
下面给出一个例子,看它是如何使用重复列表控件的。下面的例子首先在页面加载过程时把数据装载,并绑定到两个重复列表上;然后以一个2列的表格显示;最后把所有数据显示到一行上面,并且国家和领导人之间以3个中横线分隔,每一国家之间以竖划线分隔。" U4 p' w+ t Q/ N8 ~8 u# X
1.源代码(FormRepeater.aspx)2 b# m3 v$ L( X, w8 v/ x) N
<!--源文件:form\ServerControl\FormRepeater.aspx-->
5 q1 ^. q! Y+ g& z# I, h: f. y<html>
# c! i) d; C" {: {) b _2 C9 z <head>
) F" j, }" G- i. y2 c1 ^ <script language="vb" runat=server>
7 W' X3 F5 k& V
& u6 p! ~3 X/ v: v Class Leader
9 I1 Y! K5 u# m2 T' t0 z j+ n '定义一个类Leader& i! ]/ X2 f3 A' u+ f' p
dim strCountry as String
; ?( ~8 H' O& \/ P) [ dim strName as String4 h, _" P/ `% G# s% ~0 n+ ^: ?
Public Sub New(country As String, name As String)# d1 K. m$ f: B, Z
MyBase.New( n& G7 B" d! J: P* b8 R
strName = name
7 G( r0 \# ?: j, g( m strCountry= country
9 o* w0 B( Z; q1 d2 I( A/ C End Sub# }4 u# h' a( F
ReadOnly Property Name As String
2 \" o+ O- R: a! p& z# g' M; n Get
* L, Z) p" h3 [ Return strName, p7 O" |; t( m
End Get" g. y9 Z6 u6 r: m
End Property
, j5 Y, {- b: o* l
0 A; d+ S H4 [4 X3 i' v ReadOnly Property Country As String
5 x% Y, B4 j" N5 E Get 4 }# c! d5 R& h, ~% D! I7 [$ Y
Return strCountry
. X: J, ~" J& R* P% k End Get0 {( f" `! Y# u' Y+ F2 V
End Property6 j1 F1 S/ u6 y, y2 g8 z
/ P% I9 t+ {) n: ]1 C- O
End Class
' Q" N. V/ U! n5 H% D4 M
( G0 ] q& I1 [6 a1 ]% m1 ~( b sub Page_Load(s as object,e as eventargs)3 Z1 M k% m+ W& O0 V- |/ Z6 b
dim leaders as ArrayList = New ArrayList()# M- S, l+ w) A
if Not Page.IsPostBack( d2 Y0 n# m& Q. w+ Q" }
'加载数据
; d# |( z+ J+ o9 t6 Y2 W; Y+ Q leaders.add(new leader("美利坚","布 什"))
7 g9 j0 D# }0 y6 ?# z3 A! \% c2 I leaders.add(new leader("俄罗斯","普 京"))( Z! N" _: _" t/ s! s+ Z( {3 P; |( E+ J
leaders.add(new leader("中 国","江泽民"))2 Z, l# }( z4 H6 W7 N4 p
4 H0 K2 L0 M# ^ Repeater1.DataSource=leaders8 \, a3 T R1 F8 l: l& {
Repeater2.DataSource=leaders
+ S9 \& j5 K; d Repeater1.DataBind
9 M" P) G5 d3 a9 N8 |& t3 c Repeater2.DataBind8 s8 ?$ e( h/ U( m& j0 u
end if
" y E4 j& ^+ B* O5 l end sub3 M/ P/ Y7 N% x7 Y7 o" J. T) p: f
</script>
) ]3 q0 B7 p5 ~9 d) t: Y; K* g <title>
; |+ U. }3 [" P0 j, | 重复列表使用例子% H8 S* M" ?7 D+ f6 i5 [( s
</title>
. g) n/ J" W' D </head>
; b$ R. [8 ?2 g9 N2 _; t
% x) ]- A* f9 Q: [8 |, @' n5 I <center>
. s5 e" A, V3 X! ]% F8 L. @ <h2>重复列表的使用</h2>
( g8 X% P3 B) j$ | <hr>3 |% W2 ]: ~; R1 J
<br>3 l+ k3 i: i" M5 E" l: K0 ]
'以表格形式显示国家,领导人信息
8 H- S9 J8 d5 |! P& V+ f4 B <asp:Repeater id="Repeater1" runat=server>8 i" ` Z0 z; p0 E
'定义表头, _4 p4 H1 {& M' P
<template name=HeaderTemplate>
; A) `" R0 R2 w& f6 ]9 ? <table border=2>
2 d% h G$ D/ b1 d' P, X# \* q0 q <tr>* W$ v; l' Q; [8 C$ K0 z- O& l% |
<th>
: @6 X+ B9 ?1 C+ P+ ?4 X8 ^ 国家名6 E& F' \/ Q2 i5 H& z/ Y$ F
</th>
6 V, L9 j) F5 W) W <th>1 N* g$ Z3 R, @) S* {: ?
领导人
$ P6 {9 |# L) d( U </th>
$ x# R6 @5 L; h7 J: c6 Y) {! L </tr>
6 J3 o2 w2 l% ?+ @ </template>- T$ Q$ X" q" {
! t7 B9 o2 d* t9 D5 J( B ]
'定义数据显示格式
, h8 i4 r7 X) v5 F <template name=ItemTemplate>' @7 Y a% L' v4 R5 x5 f
<tr>' }! C& J9 m0 P: A
<td>
" X6 J7 q# U X" y# O <%# DataBinder.Eval(Container.DataItem,"Country") %>8 M6 a7 e& W0 L+ K
</td>
' S7 y( m0 t# { ] <td>
3 D( f1 X3 Y4 _, J1 X' k: u, o2 l0 S+ N <%# DataBinder.Eval(Container.DataItem,"Name") %>2 j. O3 k' V: ]- d3 |1 C& b5 l
</td>
) P2 U7 B& S% w* c- \ </tr>
% i( W: z5 q. z+ J; n </template>
; e4 o, d% \* p! x3 X+ P
0 D' u1 B' y/ k8 @ '定义表尾
- V1 B5 o7 E) P0 ]+ Q ]% U3 } <template name=FooterTemplate>
$ h( J& t( R' k' u6 } <tr>
1 [* W" `) w9 T4 g1 k1 a1 ]6 d' B; v <td>日期</td>+ B9 G1 r+ w; s2 Y
<td>2001年</td>5 Y' \8 M; p0 T
</tr>9 Q' `; v4 _: d8 q1 N
</table>2 Z; {# J6 `; w# M
</template>7 L0 t. L, ?7 m; `, V
</asp:Repeater> ; e, k5 ]! k Z& f/ x6 f
<br>
" A }9 F9 n( i% g L7 Z <asp:Repeater id=Repeater2 runat=server>
* B- C6 y N+ N '国家和领导人以|分割显示+ r b- } B, i# \6 y
<template name=ItemTemplate>
/ o0 ?1 \# j- ^& V6 \ <%# DataBinder.Eval(Container.DataItem,"Country") %>
6 b k8 y) S: `# ^7 `, L" | ---
" h) ?$ y$ W( u" U <%# DataBinder.Eval(Container.DataItem,"Name") %>! ^' b# A% t. `. n
</template>7 X* j) C1 W! f+ g" L6 X8 Z# o6 l
<template name=SeparatorTemplate>
2 [( ~- x& e# a! S, l |
! \! t! k n, @; i </template>
4 S- i0 `; {2 h9 X& G </asp:Repeater> i* Y/ L; ~4 T( K' T3 M8 `
</center>! n& c: ?, d' _% _1 \; ?/ U! K
</body>5 O4 A( U2 T% V, w2 u" F1 R. N
<html>
8 S. U9 Q3 `% ^; s7 V1 ?2.输出结果
$ A* G( f- Y. f5 U" D5 g+ A4 K" {$ Q% q" ~/ T9 m9 O& G; [3 w1 b$ D
2.2.14 数据列表 DataList
6 h2 {" g \3 f+ W( q9 D: U& r/ |/ _( T数据列表显示跟重复列表(Repeator)比较类似,但是它可以选择和修改数据项的内容。数据列表的数据显示和布局也如同重复列表都是通过“模板”来控制的。同样的,模板至少要定义一个“数据项模板”(ItemTemplate)来指定显示布局。数据列表支持的模板类型更多,它们如下:
* W; ^$ O; B8 }( C7 K" q1)ItemTemplate模板,数据项模板,必需的,它定义了数据项极其表现形式。; c+ e2 C- @) f" H. z
2)AlternatingItemTemplate模板,,数据项交替模板,为了使相邻的数据项能够有所区别,可以定义交替模板,它使得相邻的数据项看起来明显不同,缺省情况下,它和ItemTemplate模板定义一致,即缺省下相邻数据项无表示区分。6 J# ^8 w+ F* N- d- A# \
3)SeparatorTemplate模板,分割符模板,定义数据项之间的分割符。
7 h5 z% p2 X7 t, R$ P4)SelectedItemTemplate模板,选中项模板,定义被选择的数据项的表现内容与布局形式,当未定义”SelectedItemTemplate”模板时,选中项的表现内容与形式无特殊化,由ItemTemplate模板定义所决定。
* _4 ~$ w( z5 c( |' A5)EditItemTemplate模板,修改选项模板,定义即将被修改的数据项的显示内容与布局形式,缺省情况下,修改选项模板就是数据项模板(ItemTemplate)的定义。 K2 P9 [! I3 a. N; b
6)HeaderTemplate模板,报头定义模板,定义重复列表的表头表现形式。
- y/ Q3 B2 D% r) V* p8 Z7)FooterTemplate模板,表尾定义模板,定义重复列表的列表尾部的表现形式。
5 X. F7 v* T4 {# m& J e数据列表还可以通过风格形式来定义模板的字体、颜色、边框。每一种模板都有它自己的风格属性。例如,可以通过设置修改选项模板的风格属性来指定它的风格。2 n( z" h9 W0 o
此外,还有一些其他属性可以导致数据列表的显示有较大的改变,下面择重说明。' v2 x6 H& s3 d; C9 H6 o' ~
RepeatLayout:显示布局格式,指定是否以表格形式显示内容。4 F# G6 n' R- ^4 ?7 X: j
RepeatLayout.Table指定布局以表格形式显示。1 n( D- s. a& k! |) d
RepeatLayout.Flow指定布局以流格式显示,即不加边框。+ L+ w$ d# |3 g4 S- S& ^7 ]
RepeatDirection:显示方向,指定显示是横向显示还是纵向显示
1 @5 O1 O* b* |" p- C RepeatDirection.Horizontal指定是横向显示
7 \* i, E: ^. B RepeatDirection.Vertical指定是纵向显示7 e5 x. g* r# f% S3 [1 n8 ?
RepeatColumns:一行显示列数,指定一行可以显示的列数,缺省情况下,系统设置为一行显示一列。这里需要注意的是,当显示方向不同时,虽然一行显示的列数不变,但显示的布局和显示内容的排列次序却有可能大不相同。
5 P; z" S8 N0 y3 ~8 `例如:有10个数据需要显示,RepeatColumns设定为4,即一行显示4列时7 ^% ^5 \* M. ?- S7 Q
当RepeatDirection=RepeatDirection.Horizontal横向显示时,显示布局如下:
$ G2 l2 Q/ k, J8 i( f" A$ {Item1Item2Item3Item4) y/ W- O3 u, \0 i) f' N
Item5Item6Item7Item8
# p% b" O$ U5 F* D2 S& BItem9Item107 Y6 y) O2 R# O- c! i
当RepeatDirection=RepeatDirection.Vertical纵向显示时,显示布局如下:
3 `' y$ I& U# o4 \Item1Item4Item7Item100 u \ t) i Q. w
Item2Item5Item81 y2 R2 i( n+ t' G
Item3Item6Item9
/ |# C/ M% e3 r0 W' T# a6 h. OBorderWidth:当RepeatLayout=RepeatLayout.Table即以表格形式显示时,边框的线宽度+ P& v/ p& f+ ~3 [
Unit.Pixel(x) x>=0,当x为0时无边框
! Z0 }1 N0 J) k5 b! kGridLines: 当RepeatLayout=RepeatLayout.Table以表格形式显示时,在表格当中是否有网隔线分离表格各单元。3 M% f( I' B8 C. ]% _
GridLines=GridLines.Both,有横向和纵向两个方向的分割线。
2 @9 R* o. X [GirdLines=GridLines.None,无论横向还是纵向均无分割线。; O/ e+ B7 P; Y& k6 e3 X' k9 g
例子:演示以上介绍的各属性的设置对数据列表输出的影响,并且当数据项被选中时,数据项以粉红色来反显。1 B2 l& D. X1 z1 L( [
1.源程序(DataList.aspx)
+ D1 i; _5 `: I8 I1 w: Y% w& i' g: s<!--源文件:form\ServerControl\dataList.aspx-->6 m7 f; W- Q7 d( G
<%@ Import Namespace="System.Data" %>* ^. @. x5 s8 m) T: g4 B0 `$ S4 N
<html>
" G8 q: S$ s) v. }" K<script language="VB" runat="server">! p4 K( x% }# Z' b9 W6 M
'创建初始化表和载入实验数据/ Q& n2 X6 C* x9 i& X
Function LoadData() As ICollection
3 I& D" b! N/ |; `) f ^ Dim dt As DataTable3 M0 g# I6 r: q# K3 W: Y6 Y% j
Dim dr As DataRow
) ^. t6 w" j( m$ f: F& w' s: m9 w Dim i As Integer p- D9 V+ I6 A$ [0 t+ C& i; b
'创建数据表( W. t7 G0 K' ~
dt = New DataTable
# J W# N7 \* P; G5 J' P, t '建立数据项结构
6 A/ Q: k+ R% l( ` dt.Columns.Add(New DataColumn("Content", GetType(String)))
2 z: q* J A+ G9 o9 R5 S" q! a '载入10个实验数据
/ _- A8 u# Z# P2 P" C# G% z7 G For i = 1 To 101 U1 J1 \3 n% J* `# K8 U
dr = dt.NewRow()5 G; J0 S8 Y5 B3 m% W3 I# x5 n
dr(0) = "Info " & i.ToString()0 V3 j$ }, o1 K
dt.Rows.Add(dr)- H2 R. J d, v/ z* l
Next, m. l( w9 R8 F5 P: h4 \
'为数据表建立一个数据视图,并将其返回9 n, t6 N2 a5 X/ ~! @4 L
LoadData = New DataView(dt)- ~$ S4 ~* P2 U ~" {( m) Y
End Function' ^4 P, U5 e2 q7 f8 |4 j, L
Sub Page_Load(s As Object, e As EventArgs)
, Z& i6 t$ ?; p: n. W If Not IsPostBack Then
$ [& J$ F' M, R DataList1.DataSource = LoadData()
0 ?7 v( m4 _8 C' M3 [" _* X DataList1.DataBind+ t- @" N' S1 L z; T
End If
, i+ @% n+ L# `- R, d7 L End Sub) J0 Z$ {. H( y( E+ {# p2 \, }
Sub DataList1_ItemCommand(s As Object, e As DataListCommandEventArgs)
- J! O: K% _/ H9 p1 _ Dim cmd As String = e.CommandSource.CommandName) l" b3 k# [9 H: s7 p8 z
If cmd = "select" Then
0 H% z5 u* @! U' _! S DataList1.SelectedIndex = e.Item.ItemIndex* K- h Q) z8 m
End If
. x- }5 R |0 h6 x+ R
/ H0 \& {# I# A+ o8 ?) {' D& ?2 p DataList1.DataSource = LoadData(). |$ u( `8 q. z- B
DataList1.DataBind ! J$ g1 q: b& m1 f
End Sub% R# u5 m* M! I7 O
'当刷新按钮按下后,对数据列表属性重新设置
, ^8 F- l( Y4 R3 a0 N Sub RefreshBtn_Click(s As Object, e As EventArgs) ; N$ U( W1 b# i0 d0 c6 x
If lstDirection.SelectedIndex = 0! S* Z4 j3 T4 r: E; {3 `# G& K# F
DataList1.RepeatDirection = RepeatDirection.Horizontal6 s9 M3 T1 {9 @
Else9 ^/ }7 v- I% B2 k
DataList1.RepeatDirection = RepeatDirection.Vertical
$ P' P1 L! \1 n$ P' [+ r h End If" r* C# O+ X$ ^' N1 j# `% w4 d
If lstLayout.SelectedIndex = 0+ J* A1 c! e: [ O' s5 y# W
DataList1.RepeatLayout = RepeatLayout.Table# v. S" I1 T5 j# v# X5 _. J! X
Else/ b1 p2 e* ^! ^! ]: D/ F) C2 R6 a+ v
DataList1.RepeatLayout = RepeatLayout.Flow I9 U: k" ?" b' }
End If
/ e [, \) p5 p% `$ r+ L) d If chkBorder.Checked And DataList1.RepeatLayout = RepeatLayout.Table Then0 ^1 B ?6 }- t/ j& y
DataList1.BorderWidth = Unit.Pixel(1)' L* o3 {# m( E1 g. s
Else - ~! k+ J8 } O
DataList1.BorderWidth = Unit.Pixel(0)
# J8 s/ p/ E9 n1 o4 { End If
0 b2 G+ F, n- e
+ k0 a$ d; K2 f- [% a8 D% |' V If chkGridLines.Checked And DataList1.RepeatLayout = RepeatLayout.Table then: n! c9 z3 ?& S- @# P9 I& [
DataList1.GridLines = GridLines.Both
$ t% w" e( a5 u( }4 e Else ]% f) ?/ s8 a# B; U! e
DataList1.GridLines = GridLines.None1 P1 Q* y- a' x& I
End If
$ Z4 K; c; n7 A DataList1.RepeatColumns=lstColsPerLine.SelectedIndex + 1
' M* N* u8 _# t& Y End Sub
9 v. P& D G9 T# a6 W. d</script>
! R! L- p i. N# h, V" S<head>
?( J' `/ p9 a( e# U* O; [( q( h<title>
* X* X" ?% r. e% g数据列表实验) F0 _3 L7 }9 @, z5 O
</title>
! v( j0 X& r+ Z2 n( e</head>
. L) Q8 H& M. _7 G7 t% K7 N' T( ~<body>- {2 h" c* |) N7 D! ]
<center>
1 p0 a0 Q* I; {; ? <h2>
& I, I' c# g* V% O/ ~" ~) N% H 数据列表属性方法实验
4 G- u- }: K" B' \8 I) X4 R </h2>7 v- P2 N) U! J1 u7 R3 q) p
<form runat=server>, [/ K5 O5 X$ @9 v( X
<font face="Verdana" size="-1">
$ E4 d8 P7 K6 s) o* O <asp:DataList id="DataList1" runat="server"
, j2 F K. `( Z' E. A8 O/ X BorderColor="black"
# Z5 {5 C+ ]5 `: b( N4 g CellPadding="3"
$ s+ q& F8 g- m- v4 ]' o Font-Name="Verdana"
, g7 X& G- f. Z5 T Font-Size="8pt"' s& ~8 k1 x2 @1 F! n9 F
HeaderStyle-BackColor="#aaaadd"
4 J( k2 }) t2 y R, Y# X' I( n AlternatingItemStyle-BackColor="#ccccff"6 Y, j/ C% t/ J2 w( s% I
SelectedItemStyle-BackColor="#ffccff"
% H1 R5 h* w: r8 X" ?9 a OnItemCommand="DataList1_ItemCommand"# Y' C6 u6 Y3 s& D7 _) ^
>
% ~' D W5 j6 K# s* V- E <template name="HeaderTemplate">
5 }9 j9 U7 l3 } t' U8 S <h><center>内容</center></h>
$ n- ^# z- S- z) g4 c) |* A; G" w </template>
0 Q% j% }) Z5 V! l0 S, p <template name="ItemTemplate">7 K# a4 g3 f, `& b
<asp:LinkButton id="DetailBtn" runat="server" Text="详细" CommandName="select" />* Z/ P& q) c; d) S
<%# DataBinder.Eval(Container.DataItem, "Content") %> K6 x( Q/ P5 p" r" v2 \
</template>, ^) w. a1 a4 ~! e- L# P7 l
<template name="SelectedItemTemplate">( ^, T3 H, C3 g' a
<%# DataBinder.Eval(Container.DataItem, "Content") %>已经被选中
, U- p/ @, o5 o </template>* o g+ }. R9 X. X7 _) T
</asp:DataList>
# ?, ]" _, H0 Z5 k7 B: D7 ?) [ <p>0 h0 M# f2 S( u y
<hr>6 u. l0 l7 }' z. C# D
显示方向:! m! ?3 Z7 L& q5 Z5 f
<asp:DropDownList id=lstDirection runat="server">" V5 d3 l9 W; C' d; s- k
<asp:ListItem>横向</asp:ListItem># I, @* N' p3 w" {! O7 N
<asp:ListItem>纵向</asp:ListItem>
" {( C, f! g7 E8 T2 _ </asp:DropDownList>1 q& z) y# F: T
布局类型:/ \# Q& Q- a9 t+ O: M+ r
<asp:DropDownList id=lstLayout runat="server">
" N+ `' f U4 h/ y <asp:ListItem>表方式</asp:ListItem>5 Q2 ]+ x5 p4 ]7 H4 E: L; H$ R
<asp:ListItem>流方式</asp:ListItem>
; V2 u7 J% K6 ]7 u' Z# Y# { f </asp:DropDownList>
# K; R3 \+ L0 N/ y \9 R; a+ j5 N 一行列数:
9 g) p- }( R+ h0 L <asp:DropDownList id=lstColsPerLine runat="server">
# ?1 L; l2 ?1 T1 C <asp:ListItem>1列</asp:ListItem>( `5 b7 I+ S: y3 E
<asp:ListItem>2列</asp:ListItem>
, ?& `0 Q0 C- p7 F& r( [$ J% f <asp:ListItem>3列</asp:ListItem>3 p5 n. D3 d6 [; w, L5 |. P: J
<asp:ListItem>4列</asp:ListItem>
; Y3 M! b5 I9 U# B9 D6 f% C F <asp:ListItem>5列</asp:ListItem>. K" F& V" w6 Z" A
</asp:DropDownList>
& w* p# K0 d) h 边框显示:
. q2 F* V- C. }. i, ] <asp:CheckBox id=chkBorder runat="server" />
: h9 p. G3 k4 ^6 I0 m 网格显示:
2 G6 n. E7 o+ ~) [+ ^<asp:CheckBox id=chkGridLines runat="server" />$ Q2 ] Y* o. ^
<p>
3 e( _' I( p% X4 R) ~% c4 M. q ) H/ O+ |; f! l _: s: D
<asp:Button id=RefreshBtn Text="刷新界面" OnClick="RefreshBtn_Click" runat="server"/>
* z# P3 e3 r. a4 k' x7 g 3 z; \+ n, G/ n6 u) K& [- S
</font>/ d2 {# |8 X) w( Y
</form>( ~: @0 ?: ^4 E8 W! o- R) `6 \* u
</center>
* A; X3 v1 n. X' y4 F</body>
b: V3 I' q. m4 X5 S: {" d( {</html>% q/ i \0 I! G* Q# ?- t
2.开始时的界面显示如下,(方向为横向,表方式,一行一列,无边框及网格)
4 m( ?6 E2 k4 C$ H( K
. C5 g3 q+ a9 h1 e* V5 q/ U3.当选择显示方向为横向,表方式,一行含5列,显示边框和网格时,界面显示如下:
7 D8 `1 S: |: m- G9 h5 s
5 e) V! Y' S# ?# t% O5 Z: L9 @7 O( m( e& @
* k& u0 ^' c2 t( Y T- m
* e9 J5 ^% g. F8 D$ X0 |
9 j& m5 l/ h* q4 w) k E( M8 }! W3 O2 ] \
% N8 S5 a! V; a9 ~+ d5 K4 m4.选择纵向显示,表方式,一行含5列,无边框,无网格时,界面显示如下:: [7 Q$ U. x8 g4 D
+ o: _6 q2 I; G& I. b" m
5.当在步骤4的基础上选择了第5项数据项时,界面显示如下:
/ @& V7 t3 V: B' Y- e) O* u9 R; S: B7 E+ o9 }9 G2 b' T
接下来,我们讨论一下一种比较有实际意义的应用,即对选中数据项的修改的实现。$ q x; k M7 k4 h2 P
首先是对模板EditItemTemplate的定义,通常做法是排列可以进行修改的内容,然后定义一个修改确认键和一个修改取消键。
. f0 t; B r3 S/ P然后应定义数据列表支持的三种消息处理函数即OnEditCommand、OnUpdateCommand、OnCancelCommand(编辑事件处理、修改事件处理、撤消修改事件处理)
. C. g9 T* Q) _& q- O' ~编辑事件处理:通常设置数据列表的EditItemIndex属性为选中的数据项索引,然后重载数据列表。
/ L5 _& b5 w- j; K* Y/ N/ j5 dProtected Sub DataList_EditCommand(Source As Object, e As DataListCommandEventArgs)/ z- w1 G* B* U9 H/ I( n: S7 X
DataList1.EditItemIndex = CType(e.Item.ItemIndex, Integer)2 R: s1 G+ g4 j! G" h
‘重新加载并绑定数据, l6 F5 f; {0 O {; B
BindList()
8 z c5 q% l |( c+ CEnd Sub6 P4 M3 X% ?% E/ [1 K0 I+ {
取消修改事件处理:通常设置数据列表的EditItemIndex为-1,表示没有数据项需要修改,然后重载数据列表" _: t8 m q! N/ d( S
Protected Sub DataList_CancelCommand(Source As Object, e As DataListCommandEventArgs)# l8 D; \# V/ T! T
DataList1.EditItemIndex = -1# V4 {) P- D; O4 Y" n
BindList()- q; _, X9 X7 N( v" B2 J9 }! _
End Sub0 @) Q2 m6 J0 C
修改事件处理:通常先修改数据源的数据,然后设置数据列表的EditItemIndex为-1,最后重载数据列表。# t x0 m1 c% O5 ]$ E. u* u
Sub DataList_UpdateCommand(Source As Object, e As DataListCommandEventArgs)% S6 A; _( y* N5 O
‘修改数据源数据,应根据具体情况而变
/ ]( ]- U' r# T" ~8 X s' }+ YModifySource()
. a# K5 v" s+ }, @% eDataList.EditItemIndex=-1 |. F+ W) c- J
BindList
' N$ _/ ]! n9 Z5 D, R9 PEnd Sub% C# N0 ]: U$ F' l( r( B. b" \2 ~1 \
例子:显示一个关于书籍修改的实例。一条书籍记录包含序号、书名、价格信息。初始化数据时,我们设置序号为1-6,书名为“书名”+序号,价格为1.11*序号。6 j3 W$ f' i4 Y
1.源程序(FormDataList01.aspx). N0 _1 @9 Q0 E) D
<<!--源文件:form\ServerControl\FormDataList01.aspx-->' T: S0 Y& _) L1 }
<%@ Import Namespace="System.Data" %>
* g$ o2 W. n: h8 _" q& o4 P<html>
0 n5 s6 r8 N/ b <script language="VB" runat="server">
/ e8 L! |1 H) V6 o( c# G1 k dim Book As DataTable
, O) s/ g8 e0 x dim BookView As DataView
$ v" n" L% M( _( a7 i, s: L9 R '设置数据源,并绑定, A3 f6 H s% W( }# Z. Z
Sub BindList()
( h3 P ]/ j+ v0 ~2 Z4 }8 u4 C DataList1.DataSource= BookView6 c" A( d1 N$ @' D. _& u
DataList1.DataBind6 R# k' \* H g/ T& N
End Sub- C0 m- i: X) b
Sub Page_Load(s As Object, e As EventArgs) 0 `4 H& b( B3 \$ J# z
Dim dr As DataRow
7 @8 Z P1 R V: Z'如果没有连接变量session_book,定义数据表Book,并载入实验数据& G8 H$ _* _. R0 u) @. h8 h& O4 J
if session("session_Book") = Nothing then
$ ?) S1 Q/ }3 L* X Book = New DataTable()
8 ]4 K( b' s( k( u5 Q1 [, b* Z Book.Columns.Add(new DataColumn("num", GetType(string)))4 ?5 W: B* p4 R6 ]
Book.Columns.Add(new DataColumn("name", GetType(String)))
) [% i, _- E* f6 M) h8 r. E Book.Columns.Add(new DataColumn("price", GetType(String)))
# P N9 w( ?# ~ session("session_Book") = Book u& A9 R5 W7 D% w
'载入部分测试数据
8 m. ^ M( k" z5 x# \" t, O7 Y For i = 1 To 6: K. {( F$ m l" g* z- v0 o# @
dr = Book.NewRow()( K: P2 E& ^( w5 C
dr(0)=i.ToString; A* z0 P3 x' x9 m
dr(1) = "书名 " & i.ToString
0 Y4 K& m# r# w1 Q dr(2) = ( 1.11* i).ToString
: U" g0 f/ n5 i Book.Rows.Add(dr)
5 q. F6 r4 _( a; I Next
9 `+ p) m$ ?7 [ U% Q j '有session_book变量,直接引用, P! a: J- x% o& L* R, P& J! B
Else
0 e; g8 X0 {7 W$ w Book = session("session_Book")
5 f5 n) A! J/ v9 x: S end if
' C6 r, r+ o0 U '产生数据视图,并按num字段排序, u4 a* k$ F( [
BookView = New DataView(Book)
" ]% u/ f8 \% J& w* ]! P BookView.Sort="num"
5 b# W/ m6 g# L! q3 o4 R! ~" X '初次需绑定数据源5 M4 N' `; v- k8 o' l
if Not IsPostBack then
W3 G4 Q1 k* U: [% j; t6 S BindList
& F7 ?7 \! r4 v' p3 {; ?+ r) h/ r/ P End If3 ]/ J5 u$ a: ?. ^2 I3 O5 A
End Sub
% S/ p G `. p) m3 f+ w5 E2 V '编辑处理函数; u7 I; ]% t1 i9 C# |
Sub DataList_EditCommand(sender As Object, e As DataListCommandEventArgs)
0 a% l. X, ~+ I' O0 c# E2 A DataList1.EditItemIndex = e.Item.ItemIndex/ K4 V1 A, P6 p& G L+ H
BindList5 H W6 N# k( q' N* L/ F/ {3 c) c
End Sub
/ R& u/ `2 \, O: \" E y '取消处理函数
4 o, p- S, _* R; S% ^, p Sub DataList_CancelCommand(sender As Object, e As DataListCommandEventArgs)* G, j8 I$ ]. S7 N4 y, Q
DataList1.EditItemIndex = -14 y( M( d& B; @' O- O9 w
BindList
; D ]0 C6 D7 P" V: L- H End Sub
& S+ [/ [5 `) h4 r5 L '更新处理函数1 j' ^7 b: ~5 i
Sub DataList_UpdateCommand(sender As Object, e As DataListCommandEventArgs)
/ G+ U m4 R" Y6 s9 ^# u Dim lbl1 As Label = e.Item.FindControl("lblNum")* b7 X. v/ [" I
Dim txt2 As TextBox = e.Item.FindControl("txtBook")
/ ]0 D# U; w& u$ s7 }1 { Dim txt3 As TextBox = e.Item.FindControl("txtPrice")
8 G: B: X ]# [' ^2 b8 N" M. _
- t) i+ N8 H5 _1 i o: H dim strNum as String; }6 l7 @0 x" O6 u8 P" m
dim strBook as String
- S6 g, Y7 V% ~! M) ^: D dim strPrice as String
+ ^+ x6 j' L9 V1 v8 o9 p6 r( O strNum=lbl1.text
6 p2 a% T7 \8 E# d1 ^2 R strBook=txt2.text
/ }; _" f6 W8 Z' f: n; S2 _5 N2 ]% n strPrice=txt3.text6 \$ E7 V$ J6 `; C
'用先删除再插入的方式,实现数据的更新操作/ \" i" J* B! R0 f6 e/ B
BookView.RowFilter = "num='" & strNum & "'", f, x$ ]! G( A4 G/ r' A
If BookView.Count > 0 Then5 b( G" G0 x7 m. k
BookView.Delete(0)
2 r& `+ J, \+ m/ q0 m; W$ T End If
# }- r: c' }9 }; M* n) `, _& [9 ~ BookView.RowFilter = ""6 g: U, j2 m$ a- q3 Y) i0 y$ L
dim dr as DataRow=Book.NewRow() ' H4 d7 D7 N% c: i- \
dr(0) = strNum0 d$ T5 Y4 ]; O: k9 o9 u# ~' q$ B
dr(1) = strBook$ b" |- w: G. @$ p
dr(2) = strPrice
# h4 Q( Q. o2 s6 _4 m Book.Rows.Add(dr)
1 A7 ]0 v- s5 e$ S) c. ^ DataList1.EditItemIndex = -1( A3 v( B" l+ d2 s' [
BindList4 L& }9 K: G/ p, ~3 y4 E; [# g
End Sub
' N% Y* G! z& k) ~3 e </script>( ?8 X: [$ f0 E$ q
<head>
) { f) e: y& L<title>
5 d. Y9 R2 l. f- Z- ?数据列表修改实验
8 [' P' x- s6 h: y# V</title>
; {6 S0 B0 d2 o. E7 v, P1 K7 e</head>( M; m/ ` I! u* E6 U" P
<body>
8 a. [& ^- N Q1 R, M& n# m- |* H<center>
5 R/ S% t# c- c9 j <h2>数据列表修改实验</h2>
# M" y' y8 m2 F* v9 y: c1 I <hr>% e8 c& d/ x8 P3 H! M4 J8 D! j
<p></p>
# }/ T* }- _' Q <form runat=server>1 }/ ~: s$ X1 I& N( i7 h9 O0 ~
<font face="Verdana" size="-1">8 ^' E- y4 H3 o0 g
<!--编辑时显示绿色,并定义编辑、修改、取消时的处理函数-->
# e7 D& D0 K f8 b8 k' s( U% X <asp:DataList id="DataList1" runat="server", `' D# l2 D" J+ _+ E9 r: ?3 K; T
BorderColor="black"
9 v# q4 ?# A7 ]; E, s BorderWidth="1", t: k( \7 c$ h
GridLines="Both"
& u3 \5 a$ V+ q4 N$ q$ P$ l CellPadding="3"- e6 f+ z R |3 O) U2 ~. }9 ]
CellSpacing="0") D& v Y/ w/ D8 k) t4 {/ m
Font-Name="Verdana"
" f/ x$ ~& |+ P, a- X% W2 F Font-Size="8pt") M5 a4 s" ?; Q7 O4 N7 A
Width="150px"
, D( B3 p3 I9 P HeaderStyle-BackColor="#aaaadd"
6 R2 A1 R+ k- G1 P5 @+ T AlternatingItemStyle-BackColor="Gainsboro"
6 u- l3 N/ A3 |/ O EditItemStyle-BackColor="green"
+ i- B& D7 t2 R. ] E OnEditCommand="DataList_EditCommand"
" {) h4 g+ n5 A. r5 \ OnUpdateCommand="DataList_UpdateCommand"7 C _. h& f* {9 X+ A) e
OnCancelCommand="DataList_CancelCommand"8 T C5 s$ E$ ^0 Q
>4 k/ l/ g2 n2 p( l* i4 U) {8 f3 `" b
<template name="HeaderTemplate">3 c/ V. T3 x4 a4 a: L3 W6 T
<center><h>书籍序号</h></center>
1 T0 q1 {% x# K1 E* G </template>0 n: P. X( s4 o' @; |
<template name="ItemTemplate">
8 @& R( C- i/ y2 U <asp:LinkButton id="button1" runat="server" Text="详细" CommandName="edit" />3 M; s# C9 M( z0 V
<%# Container.DataItem("name") %>
5 _: G S( A+ v$ ? </template>
$ t% i" Q- Z' d* B8 C4 g: O6 I# V2 F" f! l <template name="EditItemTemplate">
3 S! z9 ]0 W) k- Y( R$ o5 I' ~1 ~; @/ z 书籍: 序号# H8 J3 o8 k9 G1 T
<asp:Label id="lblNum" runat="server" Text='<%# Container.DataItem("num") %>' /><br>
. R: g: i+ L0 M% L 书名:
K. J7 Z6 ?% S6 @" f <asp:TextBox id="txtBook" runat="server" Text='<%# Container.DataItem("name") %>' /><br>
( ? ]1 C4 ?; o6 ~ G/ [ 价格:4 Y3 g, D( @5 s; M% Y6 f$ D
<asp:TextBox id="txtPrice" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "price") %>' />
% ?2 [, C/ x0 ?7 [4 @ <br>, [( G2 O) M, {+ v( R: J* g' J
<center>
9 y% ?: q8 a" } <asp:Button id="button2" runat="server" Text="修 改" CommandName="update" />2 d% W( P* @3 G
<asp:Button id="button3" runat="server" Text="撤 消" CommandName="cancel" />
5 C3 e9 Q9 F9 v& U" r G </center>( ]2 X3 f0 m! y5 `
</template>
3 S% Y( s) I C9 B) E </asp:DataList>2 I4 z; f2 f2 |7 M5 A& ]
</font>
/ T( {! S/ U! W) Z! L. D! L" g1 F* l </form>9 |6 b- ^0 V; P i/ k$ N7 Z
</center>' j- w% |% Z# [$ ?9 q* C
</body>
& }; X! F! f' I4 E# a</html>
$ I3 ?" y" g5 t2.准备对第2项进行修改,此时的画面如下:
4 p+ ]* v2 S. C5 n* z
6 W* n; E% }$ a4 L/ v3.把序号为2的书籍的价格改为9.99以后,重新进入其编辑状态后,它的输出画面如下:
, `2 q; C. |4 N1 ?* b. }3 d2.2.15 数据表格DataGrid2 t, e, O: Q; {# {9 B
数据表格服务器端控件以表格形式显示数据内容,同时还支持数据项的选择、排序、分页和修改。缺省情况下,数据表格为数据源中每一个域绑定一个列,并且根据数据源中每一个域中数据的出现次序把数据填入数据表格中的每一个列中。数据源的域名将成为数据表格的列名,数据源的域值以文本标识形式填入数据表格中。
. p' z: N) L# D, b7 n1 {. j7 K通过直接操作表格的Columns集合,可以控制数据表格各个列的次序、表现方式以及显示内容。缺省的列为Bound型列,它以文本标识的形式显示数据内容。此外,还有许多类型的列类型可供用户选择。
& d* Y o3 P6 k( g列类型的定义有两种方式:显视的用户定义列类型和自动产生的列类型(AutoGenerateColumns)。当两种列类型定义方式一起使用时,先用用户定义列类型产生列的类型定义,接着剩下的再使用自动列定义规则产生出其他的列类型定义。请注意自动定义产生的列定义不会加入Columns集合。5 h; |+ t8 Y R' `1 Z* f
列类型介绍:
- `. `, l9 Q1 D) U( f1 I1)bound column ,列可以进行排序和填入内容。这是大多数列缺省用法。
4 g" N3 d: G5 l& d 两个重要的属性为:HeaderText指定列的表头显示- R( I9 u. e% W( m
DataField指定对应数据源的域0 X4 A8 Y5 Y* T& ]
2)hyperlink column,列内容以hyperlink控件方式表现出来。它主要用于从数据表格的一个数据项跳转到另外的一个页面,做出更详尽的解释或显示。
6 C0 _2 z% V3 p( T) }重要的属性有:
2 ?9 O+ J* B- [) R. A) WHeaderText指定列表头的显示
, l3 ^4 ]* ~, |+ oDataNavigateUrlField指定对应数据源的域作为跳转时的参数1 v4 y- @# Y6 j8 b4 A
DataNavigateUrlFormatString指定跳转时的url格式
) C, g7 N$ F. U7 z! gDataTextField指定数据源的域作为显示列内容来源2 K3 t: H4 ^: g; N& S. }8 r) }9 A0 A' ?6 i
3)button column,把一行数据的用户处理交给数据表格所定义的事件处理函数。通常用于对某一行数据进行某种操作,例如,加入一行或者是删去一行数据等等。
% a. o% h* ?' b重要的属性有:
# @; k0 g1 `9 `) K L1 p" }" E0 |, HHeaderText指定列表头的显示
, b9 R' T% ~3 k0 p$ u% c/ b0 HText指定按钮上显示的文字
. N- {0 ~" Q( {/ z1 K/ Y1 YCommandName指定产生的激活命令名
% J- i$ y) p6 F( C5 `4)Template column,列内容以自定义控件组成的模板方式显示出来。通常用作用户需要自定义显示格式的时候。
9 K* f2 s) I5 ~5)Edit Command column,当数据表格的数据项发生编辑、修改、取消修改时,相应处理函数的入口显示。它通常结合数据表格的EditItemIndex属性来使用,当某行数据需要编辑、修改、取消操作时,通过它进入相应的处理函数。例如,当需要对某行数据进行修改(update)时,通过它进入修改的处理步骤中。
+ b9 B9 j y+ D- k- y c: \其他重要列属性介绍:
7 j1 M3 W9 u8 q( N) o! J" P& ^% B: }1)Visible属性,控制定义的列是否出现在显示的数据列表中。
+ h3 n8 H! G( u5 A, ?8 E3 |2)AllowSorting属性,是否可以进行列排序。当AollowSorting=true时,可以以点击列的列表头的方式,把数据以该列次序进行排序。缺省的(即载入数据后)的排序方式,实际上是以数据在数据源中的排列次序进行排序的。
* o3 ^6 T1 H4 x: l5 M) I5 A" M3)AllowPage属性,是否以分页方式显示数据。当对有大量数据的数据源进行显示时,可以以例如10行一页的方式来显示数据,同时显示一个下页/前页的按钮,按下按钮可以以向前或向后的方式浏览整个数据源的数据。当AllowPage=true时,即以分页方式进行显示。可以通过设定CurrentPageIndex属性来直接跳转到相应的数据页。
" V* e0 f$ E6 d! t V. m6 ?* b例子:演示以上各种类型的列定义的用法
3 i' I- v0 |) x1.源程序(FormDataGrid.aspx)( y( O* }+ _ p& M5 v
<!--源文件:form\ServerControl\FormDataGrid.aspx-->
- d8 e# X1 a. o1 S- Y0 [* q9 r<%@ Import Namespace="System.Data" %>; r5 F, H6 J( k Y5 V7 g
<html>
2 I2 ?) L. }; V7 v' b<script language="VB" runat="server"> A; {) ?" S5 V$ b5 a9 u0 z4 U
dim Order as DataTable2 E3 C, t3 l8 n0 a( d' ~& w9 ?
dim OrderView as DataView; g4 ?$ G4 M- H& O3 c2 v7 U) j
'对数据表格1创建数据表,并返回数据视图
" {2 z* j5 ~% R9 b& C6 J) N% U Function LoadData() As ICollection/ a4 F# y( r4 u% u/ l! L
; {# Q) G* u; g
Dim dt As DataTable5 n& I0 F* q* |. K1 K
Dim dr As DataRow
. u* S" Y# w2 u Dim i As Integer& F# F0 s* J- c( C% D2 O6 l9 d6 \
'创建数据表
! [$ _4 A% p6 k2 k3 `& _ dt = New DataTable2 o' i4 U% |9 R* ?4 d4 L: \
dt.Columns.Add(New DataColumn("Num", GetType(Integer)))7 D. J3 w2 Y0 F
dt.Columns.Add(New DataColumn("Name", GetType(String)))! e' h! V/ t+ b; U: \9 J$ U3 P
dt.Columns.Add(New DataColumn("DtTm", GetType(DateTime)))
" D0 m: N! @% I6 J dt.Columns.Add(New DataColumn("Assembly", GetType(Boolean)))
/ q7 V; k+ x' F* h dt.Columns.Add(new DataColumn("Price", GetType(Double)))
5 D$ N. w3 v9 r, b '载入数据 r5 l0 r9 [3 B3 Z3 ^
For i = 1 To 6# p0 b8 g4 f2 k3 s* f# k
dr = dt.NewRow()# W3 O) y$ O# j V5 w
dr(0) = i
6 E" }( G) _- V' V3 [ dr(1) = "书名 " + i.ToString()
9 R/ H4 i- @) v; a( n7 } dr(2) = DateTime.Now.ToShortTimeString
* r' N- ]4 t1 b1 M6 L1 d- K2 P3 G# b: A7 M If (i Mod 2 <> 0) Then" j8 X! r M) _- p/ y# z) Z3 v
dr(3) = True( o* L* A# C- L5 j4 Z: _
Else7 Q& Z4 Q2 l' v% k1 T. h7 f3 B
dr(3) = False
6 s- r0 g' j+ }% P3 Z9 D( z End If) n- i6 K0 A1 J5 I+ t
dr(4) = 1.11 * i! E. D. b3 N, L) [3 M
'把产生的数据加入数据表中* H' ~' c. {- x9 g/ h
dt.Rows.Add(dr)
. |* r1 I, o- Y# M1 [; k4 a& z3 I+ g Next$ l) n! Q; g1 m# L( v: f
8 @) m0 C2 Q- F$ c+ z+ ~5 B
LoadData = New DataView(dt)
|; X8 Z% Y$ ^- O J( {4 m% M & I6 G: A/ _) m0 T1 x- [3 h" t: J
End Function6 o: M% R' h" F# z3 w
'页面初始化,分别对DataGrid1和DataGrid2绑定数据源
: B4 s5 l5 n ]+ }! G Sub Page_Load(sender As Object, e As EventArgs) $ C/ ^9 p2 y( ]/ j9 ^1 N* m W
1 @: x6 ` \ m; C H7 l: h4 }7 } If Session("session_order") = Nothing Then3 {. m5 o. e7 ?' t4 q
Order = New DataTable()
1 t. n* _% K) O* ] Order.Columns.Add(new DataColumn("Name", GetType(string)))
& R* b( y: `3 r9 f0 R7 ?8 Y Order.Columns.Add(new DataColumn("Price", GetType(string)))
0 R4 W7 U2 A9 t. S1 ]) C Session("session_order") = Order
7 \- R3 m E7 P/ u# y v Else
3 n/ |; P1 ?( ?4 a5 O/ W Order = Session("session_order")( @# s& {* W! z, v: C v9 m6 G
End If
9 Y9 J8 h% c6 b4 Y OrderView = New DataView(Order)9 I0 |. l1 v" J, C2 G; Y7 e0 ]
DataGrid2.DataSource = OrderView S4 J+ L6 B) ]- q4 p+ E
DataGrid2.DataBind
# i ]3 F2 B2 ?3 ~ If Not IsPostBack Then" R# b$ P1 U( u
DataGrid1.DataSource = LoadData()3 M# m0 R6 L1 o4 ^: }+ \
DataGrid1.DataBind
. x6 F8 _# l. c8 D8 R0 M4 N End If
" J% H2 ]* @9 I8 @ End Sub J4 k; K! t Q
'对ButtonColumns的处理函数集合
' }7 a+ o% c- y$ N$ |& B Sub Grid_Command(sender As Object, e As DataGridCommandEventArgs) - x# S- _ ]* a3 u! k+ U( O, @
: R5 P4 O! }8 O$ k5 n0 t' m; G1 w Dim dr As DataRow = order.NewRow(), Z% D3 {7 k& x1 b8 ?, L# M4 a
- ?& t' i3 Y# L0 G6 C
Dim Cell1 As TableCell = e.Item.Cells(3)
5 u9 g& A6 x4 m8 F+ A! u Dim Cell2 As TableCell = e.Item.Cells(6)& P6 r# e" i0 [( b2 ^6 d* R6 Z
Dim name As String = Cell1.Text
$ e, w N6 Y/ { Dim price As String = Cell2.Text& b* k% ?" ]1 {, M. O& P
/ D( l* V. O$ C' V, t2 Y$ J If e.CommandSource.CommandName = "Add" Then
r% {/ B2 d$ Z7 m- `! I C+ P dr(0) = name
; b0 j' v# M5 J) v dr(1) = price T3 x1 H! o$ X
order.Rows.Add(dr)
" W# i4 f- J$ l! R1 m7 \2 w Else , j# m5 k1 r ]2 @
OrderView.RowFilter = "name='" & name & "'"
* S' P+ ~: d# b& _; x If OrderView.Count > 0 Then
0 b* N. L2 D+ [9 ~ OrderView.Delete(0)% N) _: b, L% M% a4 |7 y& I# y
End If
2 u/ a1 F9 W5 B1 A1 Z OrderView.RowFilter = ""
4 c& S2 l; E- W& L) z End If
+ F; i, ~8 l0 ?+ Y) | DataGrid2.DataBind()/ n" Y) B1 o- Q( l# u! Y8 @
End Sub
9 J) M* @# F% L</script>
$ p* A3 X! r5 s7 r<head>% e* s( F; l8 h0 D$ n4 B ]$ ?% t
<title>0 t3 j! {! y% [+ w
数据表格实验
7 Y- i6 i# q. N8 j, s8 U</title> f* e1 r9 I8 `* X" C+ Q/ g; q
</head>
, H1 J) y# I, w$ W: _<body>* D' s" m) V& E- w# x
<center>
; o6 q s* K2 { <h2>数据表格列类型实验</h2>: H# s5 t& l8 W8 T' Z( h
<hr>
0 X0 v# e% w! b: K( e$ A <p></p>$ H2 y9 S' h. y. H s# x4 M
<form runat=server>
2 F8 l, r- \- l) d+ Y, w0 o: v/ L <h3><b>图书清单</b></h3>
+ H; i: C& \7 ~) Y& t/ M <ASP:DataGrid id="DataGrid1" runat="server"
0 |( ^7 f* Q' z9 N7 s BorderColor="black"
0 b1 _' r( X7 {6 t# F3 ?7 X' C BorderWidth="1"
3 M4 H% ?+ m) z8 G7 k- C1 s GridLines="Both"
4 g1 v2 G5 V' K# A CellPadding="3"
$ j* x, y3 f) e0 |9 L CellSpacing="0"( l8 i4 K5 Y! e% j& [$ R
Font-Name="Verdana"
* w; N# c2 i2 p9 M Font-Size="8pt"# d( `4 X( H: o8 k
HeaderStyle-BackColor="#aaaadd", Q4 d0 n0 Y0 c, ]+ y
AutoGenerateColumns="false"
. @: m6 U; z7 G OnItemCommand="Grid_Command">
" X% L7 R' `! @ <property name="Columns">4 e2 S4 c" f/ X; b
<!-- 2个ButtonColumn示例-->% G5 F7 q( g2 S- t
<asp:ButtonColumn HeaderText="操作" Text="订购" CommandName="Add" /># B+ Q/ I$ S5 O3 |, s" A* o
<asp:ButtonColumn HeaderText="操作" Text="退订" CommandName="Remove" />
/ U s5 \1 p0 R# c8 o" O3 d) E* k <!-- HyperLinkColumn示例 -->6 a$ |& l/ ^4 B3 P- F% m0 Y
<asp:HyperLinkColumn$ X# A! X& o4 Y/ `2 R
HeaderText="链接"
0 Y' }/ U1 }" a9 ^% X! c' w# N' r DataNavigateUrlField="Num"
* t' L' Y7 |+ F& s- ]! R! ` DataNavigateUrlFormatString="FormDataGrid01.aspx?id={0}"6 V# F* g5 l. {5 ~7 A
DataTextField="Num"
$ k" b$ m& P8 t Target="_new"
4 K, e. v" E( }9 l+ K* ?, o />; C5 Q9 X5 P" M: a# o$ c
<!-- 2个标准BoundColumn示例 -->$ }, P$ F4 `* H8 j* t
<asp:BoundColumn HeaderText="书 名" DataField="Name" /># E: I2 A7 m' f% S0 n0 V
<asp:BoundColumn HeaderText="入库时间" DataField="DtTm"/>
; \- i8 a. r l5 p0 m <! -- 1个TemplateColumn示例 ,以CheckBox来表示布尔型数据 -->
4 @1 n- m! w* [" u* v <asp:TemplateColumn HeaderText="合 集">
) D2 @4 d* X/ _9 W* S& b <template name="ItemTemplate">1 Z* R& u6 |" v _! S2 f
<asp:CheckBox ID=Chk1 Checked='<%# DataBinder.Eval(Container.DataItem, "Assembly") %>' Enabled="false" runat="server" /># I' U8 ~" M+ n0 t7 e
</template>
- \4 i9 `0 G1 [9 i </asp:TemplateColumn>, \& Y9 p1 _, J: K
, t3 B8 b8 n5 u N
<asp:BoundColumn HeaderText="价 格" DataField="Price" DataFormatString="{0:c}" ItemStyle-HorizontalAlign="right" />% y; d9 I" p0 [2 `1 D- q# V
</property>) f% J" o8 P/ X8 E+ {2 V. ]
</asp:DataGrid>
% B) o( k" D Q& Z; o9 G% o <hr>
+ K) K- |4 W! j+ U; X4 O2 ]7 H; L <h3><b>订购清单</b></h3>3 S( {% A! ^+ B- w6 o
<ASP:DataGrid id="DataGrid2" runat="server"# R' c( _+ l5 ^5 a9 [6 Y$ F
BorderColor="black"
3 L4 {4 |/ u' G. w# v BorderWidth="1", O7 Y$ U5 `$ `; `
CellPadding="3"2 o* ]9 {5 a% |6 Q3 D6 ?
Font-Name="Verdana"
5 u+ @, \ ^8 h7 C$ w* ? Font-Size="8pt"
8 Y/ _" y' I1 c2 g& p8 J HeaderStyle-BackColor="#aaaadd"
7 T& A8 n4 [% `, ` />$ D9 g! t9 j5 x9 ~: _; Z! ^8 M
7 D% L; G8 E2 `/ V) x4 i7 W+ s
</form>
5 y/ d7 b3 L/ Z; ~' r/ U+ b</center>
1 h/ {, b8 F' E6 H" j9 P1 M1 {</body>6 y* i J3 T6 b& S
</html>
6 C: E$ x* g2 g. \( ]: x: J0 R( B, p文件FormDataGrid01.aspx的内容:3 }6 w7 H6 l3 S- d( z; f$ { a
<!--源文件:form\ServerControl\FormDataGrid01.aspx-->- T; r* m3 P+ r8 x5 U% \
<html>6 m: m' d. ?) ~! \5 s/ ?* s
<head>
/ Z, T+ |, D- b w; Q- a: _<title>8 e1 K. V+ y- p# h& G5 y, @
数据表格链接测试实验
2 w1 }$ V! q- O* ~! l</title>
$ C" ]4 y: F5 O0 W<script language="VB" runat="server">2 p" B9 e( k# w. [( w% o# e* {3 Z# q
Dim num As String+ H$ H# M: P- H% n, a8 \
Sub Page_Load(sender As Object, e As EventArgs)
9 `" R* s7 J* C& U. k) ~! q4 K num=Request.QueryString("id")
' J# P8 Z0 f7 H0 | }2 V End Sub
( @* l# P: \) e( |% _& ?( q8 E</script> {8 P2 M7 P7 X; V# v
</head>
7 r0 E2 f( G7 L3 ^<body bgcolor=#ccccff>- C5 i0 L; d) A$ m, K3 W3 E1 s
<center>
, {% c2 O/ P# c. l <h2>数据表格链接测试结果画面</h2>( W! G" c: }% k1 x+ b
<hr>
* N4 J7 d! I. d <p></p>% [( B2 I- B4 o3 D0 J
<h4>您选择的是 第<u> <%= num %></u>本藏书</h4>
; _: M& T5 `9 k. Q- y</body>' m) L/ w- |( X P' s0 L
</html>
5 r# |: r# L! ~2.开始时画面:
: s+ K H) `, V! F: B9 Q) n. D' {# z8 _
. @# q' U# s# W3.当选择订购了第一本和第三本后的画面如下:% [, b1 } Y! F8 A% E" j! O: Y* {
+ A7 i% s/ ~( t5 f: q2 h2 {9 |' z' N# H4.当选择退订第三本书后的画面如下:
$ z' a7 j9 c- L/ @4 G
) Y) k2 T4 B% {5.当点击连接第六项时的画面如下:) @- u, ~4 i3 d' s3 _
2.2.16 小结
0 \! T. b5 K, }% _% H8 m本章主要讲述了几个服务器端的控件、它们的校验、取值方法等,从中我们可以看到asp.net中各种控件功能是非常强大的,如上面的例子所示,我们甚至可以用一个简单的语句就可以验证输入的合法性。对取值,我们也有简单的方法,对比于用html所写的代码,我们觉得用asp.net所写的是简单了很多。
3 w2 L4 T Z1 m第三章 自定义控件
( l$ Y9 b7 B) G+ K. u, ^7 easp.net中提供的增加内嵌服务器控件的功能,使你能够多次的轻松增加你所定义的各种控件。事实上,对于表单等各种控件,可以不用更改或者稍微更改一下就可以多次使用的。在通常情况下,我们把一个用作服务器控件的web表单统称为用户控件,我们用一个.ascx为后缀的文件保存起来,这样的保存使得它不被当作一个web表单来运行,当我们在一个.aspx文件中使用它时,我们用Register方法来进行调用,假设我们有一个文件名为saidy.ascx的文件,我们用下面的语句来调用它:
% [3 o4 [/ I7 q6 {. ?7 G3 c<%@ Register TagPrefix="Acme" TagName="Message" Src="saidy.ascx" %>% K$ [( B! I* ]8 @5 q0 M
上面的TagPrefix标记为用户控件确定个唯一的名字空间,TagName为用户控件确定一个唯一的名称,你也可以用其它的名字代替“Message“,Src为确定所包含的文件名称和路径。这样,我们就可以用下面的语句来调用它了:6 s( c2 ]5 _5 t, M$ B; H' k4 ?
<Acme:Message runat="server"/>9 Y! w* R7 @0 c) @$ \% t9 K
下面我们来看看具体的应用- x# P% ?) c- f5 j/ q
2.3.1 小页面控件2 r5 a( u3 |# v: P% F5 K# {
我们建立两个简单文件来说明这个控件的使用方法:con01.aspx、con01.ascx,在con01.ascx文件里我们只有一句话:4 d: y$ x: w# N
<a href="http://www.yesky.com">欢迎访问天极网站</a>3 J* k- O( P( `; b+ r
然后我们在文件con01.aspx里面进行注册:* S2 }& }) ~) B; U# c, R
<%@ Register TagPrefix="saidy" TagName="info" Src="con01.ascx" %>& V' W% U$ @! V) N7 P0 J' @* q. M
页面上的应用我们用这句话来表达:5 v! Y& o1 A! g4 m( I$ j% b
<saidy:info runat="server"/>" C* X/ P* w$ [' L Z
con01.aspx文件的完整代码如下:
9 }. a; [& ~4 H% s' u1 K% H5 W) r! Y& \<!--源文件:form\CustomControl\con01.aspx-->7 r5 F; Z% T% V2 E
<!--注册小页面控件-->
$ O9 t, Y( E0 e/ {& y$ I<%@ Register TagPrefix="saidy" TagName="info" Src="con01.ascx" %>7 ^3 {+ ]5 I# L, L2 D, C. N
<html>1 O! m+ ?3 A% s
<body>1 k/ V ]: K3 R/ A: f4 _
<BR><BR><BR>9 v' a1 `& u' T# |! O1 q7 {% d
<CENTER>+ }; M X0 t/ ]; c8 s3 M
调用结果
; w' q, L7 x/ r& {<BR><BR>" H3 p- u' z* Z, C' }: x
<saidy:info runat="server"/>
& s! t2 ?+ W$ D u7 Y4 P</CENTER>
" L( q9 }( d" T# P<BR><BR>
& t8 G6 y# q! T/ U9 V1 b, M</body>/ d X4 C2 A/ k5 L9 j- F _
</html>4 Z! N( ?6 ^( Y, l+ F8 E( _& i
下面我们访问con01.aspx,显示如下:3 r* _/ n% a( q3 X; @3 f& y2 q
2.3.2 代码和模板的分离; v1 ~2 f: ~$ e( R* U" O( N
在编制asp.net程序时,我们会使用模板(Template)。那么什么是模板呢?相信大家都使用过WORD,当我们在新建一个WORD文件的时候,我们可以建立模板。通过使用模板,我们就固定了文档的风格,这样就可以在模板上完善我们的内容。所以我们使用模板一个好处是:文字录入和编排界面是分开的。而且模板可以重复使用。好了,通过上面的介绍,我们对模板就有了一定的认识。 我们在编制.NET程序时,使用模板将对主程序代码大大简化。模板的定义是使用<template>和</template>标示符的。文件保存为.ascx文件。下面的代码是一个典型的模板的定义。
+ b8 s3 ^( K0 ^& q" e+ r: ?8 @ <template name="itemtemplate">4 C6 [$ n k" _/ d1 r# J
<table cellpadding=10 style="font: 10pt verdana">+ O9 B$ y/ W6 {9 Y
<tr>7 c J+ k2 u1 P1 |: N$ e: m
<td valign="top">! S) P8 M. u) f/ v: W/ f6 i
<b>所在系: </b><%# DataBinder.Eval(Container.DataItem, "dept") %><br>8 A9 T" [# Y) T; X9 {
<b>姓名: </b><%# DataBinder.Eval(Container.DataItem, "name") %><br>
% j* D4 s8 x3 C H0 I <b>性别: </b><%# DataBinder.Eval(Container.DataItem, "sex") %><br>
5 F- X/ `1 M; g <b>年级: </b><%# DataBinder.Eval(Container.DataItem, "grade") %>
0 r- @' u, g: X+ I: f; t( y </td>
1 i4 j/ F. ~3 L$ q: J$ ? </tr>: M. y- d S; V. W7 A
</table>
" }) t+ t/ p4 l4 A' S4 B' C </template>5 u4 K [* B, a2 i- ]
在这一模板中,我们使用了数据绑定控件,关于数据绑定控件,请参阅其它章节。同时我们还定义了数据的显示方式。那么在主程序中如何调用呢?请看下面的代码:/ V$ H, K- R1 m& i* d4 e# n
1.<%@ Register TagPrefix="Acme" TagName="StuList" Src="form32.ascx" %>
$ S3 N+ |* r/ c# c2.<html>& O5 ~0 `) s+ q) U) p8 x+ ]
3.<body style="font: 10pt verdana">- N" l: ^- U/ ]) u
4.<b><center><h3>模板示例</h3></center></b>
! K( F; C# l% A7 i' B5 v5.<form runat="server">8 }6 c! @6 i( l
6.<Acme: StuList runat="server"/>8 B# [$ A k3 [3 y' N# E
7.</form>. y* e! b6 K8 h$ ?- A
8.</body>4 E- z9 n) w- `* h- ~/ I
9.</html>5 ?* U r) j% N
其实,模板也属于自定义控件(User Control),所以我们在使用时,要先注册(Register)。对主程序的第一行代码,TagPrefix定义了一个不重复的名字空间(Name Space)。TagName为自定义控件定义了一个名称。然后,我们就要指明使用的模板的文件名。注册完自定义控件后,我们就可以把此控件认为是服务器端控件。要使用服务器端控件,我们要做什么工作呢?对了,要使用runat=”server”属性了。请参考第7行代码。
+ f; U o3 X( a3 h0 S, S0 S 好了,现在我们就看一个完整的例子!这个例子包含了两个文件,一个主程序文件(template.aspx),另一个是用户自定义控件文件(template.ascx)。先看template.aspx文件。
+ s2 l3 R: @6 g1 \; R<!--源文件:form\CustomControl\template.aspx-->( x' S" s9 q0 n! ^( v l/ Z3 }
<%@ Register TagPrefix="Acme" TagName="stuList" Src="zy.ascx" %>
4 a- A! u$ k" ]- G' A, ` <html> }' y/ R [$ x
<body style="font: 10pt verdana">8 z3 {3 [) K" O! r0 o% c
<b><center><h3>模板示例</h3></center></b>
3 @8 |, O6 i0 M O <form runat="server">- P! Y$ }4 z9 s' X
<Acme:stuList runat="server"/>$ p1 b; m7 g( U5 Q$ \* ]/ \
</form>
0 a8 {% t9 s k- d5 ?* F</body>
d1 j7 [ O& s</html>$ [7 S# E* r h! Z5 u4 l# Q; Q
现在我们再来看template.ascx:$ D) r7 Z) {: y4 @3 _, ~+ R1 n) i
<!--源文件:form\CustomControl\template.ascx-->
X [: a3 G3 v2 l7 n" {4 t<%@ Import Namespace="System.Data" %>- h0 u5 z9 N V- k" w
<%@ Import Namespace="System.Data.SQL" %>+ O( l$ w4 [$ d9 V+ i+ B7 t
<script language="VB" runat="server">( @* R8 B K( ^4 N
Sub Page_Load(Src As Object, E As EventArgs)
5 I" A; j/ r+ o4 j( n) A If Not (Page.IsPostBack)
: @" V2 {; n. x5 @* S# X Dim DS As DataSet+ Z* M. T; D4 G6 G0 O
Dim MyConnection As SQLConnection
+ r; _9 i. L4 q n" J Dim MyCommand As SQLDataSetCommand1 E+ b) _& Z( P
MyConnection = New SQLConnection("server='iceberg';uid=sa;pwd=;database=info")* O6 P4 Z: l, ]7 S* j7 J
MyCommand = New SQLDataSetCommand("select * from infor where dept='" & Category.SelectedItem.Value & "'", MyConnection)
* k; {; V- O' m4 ?$ G2 u* a7 ] DS = New DataSet()
' a* f4 }7 u: Z0 z" I9 w MyCommand.FillDataSet(DS, "infor")4 y& h8 E0 @ w1 J+ p
MyDataList.DataSource = DS.Tables("infor").DefaultView
0 b+ x. u4 p. T: s W# J) w" ^ MyDataList.DataBind()! G0 b! E$ X. [: M0 d8 x* l
End If; ?1 h0 W+ m8 L
End Sub
# }4 N) p) l) W Sub Category_Select(Sender As Object, E As EventArgs)% i W, Z. h' ^- ]. o
Dim DS As DataSet2 v+ k& N2 ]0 _3 m9 H
Dim MyConnection As SQLConnection
7 ?+ D9 h9 B* w+ o1 Q2 u Dim MyCommand As SQLDataSetCommand
% w+ q4 w, }6 d E MyConnection = New SQLConnection("server='iceberg';uid=sa;pwd=;database=info")# _6 c) L" \( P) n& k2 l$ e3 \
MyCommand = New SQLDataSetCommand("select * from infor where dept='" & Category.SelectedItem.Value & "'", MyConnection)
/ d+ i. D( e7 d DS = New DataSet()
* F$ Q4 j+ _0 P" ^4 \5 [" @ MyCommand.FillDataSet(DS, "infor")
. A5 h1 h" o( N6 D B+ a% p2 e0 K7 Q4 p MyDataList.DataSource = DS.Tables("infor").DefaultView: |9 E2 A) b, n. B) I
MyDataList.DataBind(): a, k; B/ Z$ C1 C' ^0 j
End Sub
5 A' A2 E( J- ]# i8 P( e! V</script>
5 _* p) p; N( B3 `4 y1 K1 o<table style="font: 10pt verdana">2 a5 h) M$ j4 V V/ S
<center> 0 b: X0 Q+ f/ o$ _6 y( o5 j X) |& x
<tr>+ G" S! x# U$ S
<center><td><b>请选择系名:</b></td></center> 1 r9 @0 I e7 w& g
<td style="padding-left:15">
! b# u5 {& g7 s0 _! V<center> <ASP:DropDownList AutoPostBack="true" id="Category" OnSelectedIndexChanged="Category_Select" runat="server">8 h) Z/ D" A; ~+ S: ~ \% N+ v
<ASP:ListItem value="信息系">信息系</ASP:ListItem>
- M4 G' H3 J- i# X6 C' t4 c/ x <ASP:ListItem value="工程系">工程系</ASP:ListItem>9 j1 @3 s; M! y. G
<ASP:ListItem value="英语系">英语系</ASP:ListItem>0 D; g- D; e) ~% F, D: C* A
</ASP:DropDownList></center>
" e# I) l! X# T2 l* _* \# i % p) S+ R* e W0 \
</td>
h* _& w& U. W7 _* i6 K </tr>' S* n1 j" S" ^* w) D; V
</table>- I5 C4 H# s9 f# N5 R& u
<ASP:DataList id="MyDataList" BorderWidth="0" RepeatColumns="2" runat="server">. ^1 n8 c$ R3 H" _; U7 j
<template name="itemtemplate">" ]& p5 B8 e" @0 c: A
<table cellpadding=10 style="font: 10pt verdana">
1 t7 p3 t) C0 a# W7 I <tr>$ m, E4 D3 O/ r
<td valign="top">
. H, f5 m+ R3 Q6 F <b>所在系: </b><%# DataBinder.Eval(Container.DataItem, "dept") %><br>3 Q$ h" R4 u) m! N6 m
<b>姓名: </b><%# DataBinder.Eval(Container.DataItem, "name") %><br>1 V z$ \) s* U7 b, f! P' q8 @
<b>性别: </b><%# DataBinder.Eval(Container.DataItem, "sex") %><br>
) K$ J6 V& ?$ K8 I/ c: ?0 C& @7 r3 m _ <b>年级: </b><%# DataBinder.Eval(Container.DataItem, "grade") %>
/ m5 ]9 f v1 G: B </td>
! G; X3 J. w, T. s </tr>1 L" F. D, r4 d' h
</center>
) s+ I& b6 Y& {) N</table>* L% D4 z4 b* `0 [* s
</template>$ h% o5 k/ B4 b
</ASP:DataList>
9 g( {; p% J s- I' {运行的效果图如下:1 Q$ v, C% j# l+ h' V+ K- [
这样,一个完整的例子就做好了!实现了代码和模板的分离。试一下吧!
# n# K+ j" ^8 F2.3.3 自定义控件7 _2 P" p) e: v; o+ a- B, q: I
在asp.net中,除了我们应用的服务端控件之外,我们还可以创建自己的服务端控件,这样的控件叫Pagelet。我们来介绍如何创建一个Pagelet,这个Pagelet的功能是在被访问时返回一个消息。
# e) y7 e( j$ _- m我们创建一个Pagelet,用来返回一个消息在客户端的浏览器上:& W! ~ _2 X/ ~
Welcome.ascx:
: A; p$ B5 i4 i/ Q+ ?' [<!--源文件:form\CustomControl\welcome.ascx-->
- }0 L1 N' M5 t, V9 @欢迎来到我这里啊!!!6 H7 Q' p/ B8 q3 X* g
就这么简单,当然你也可以让它复杂一点。当一个Pagelet被创建后,我们就可以通过下面的记录指示来调用它:
+ i9 }; T; ~+ O' K<% @ Register TagPrefix="wmessage" TagName="wname" Src="Welcome.ascx" %>! {# a3 Q2 j- s! o2 ]* d
TagPrefix为Pagelet指定一个唯一的名字空间,TagName是Pagelet的唯一名字,当然你也可以换成其他的不是”wname“的名称如:TagName="saidy"。Src属性是指指向Pagelet的虚拟路径。
0 `( I6 S3 r) m; J d p: q- C一旦我们注册了Pagelet,我们就可以向用普通的空件一样来应用它:
9 u7 l# ~2 Z* K% l2 I<wmessage:wname runat="server"/>
! H+ ~) [7 ^: J下面的例子示范了自定义的控件的应用(welcome.aspx):
9 A- L( V- _( x2 A/ e<!--源文件:form\CustomControl\welcome.aspx-->+ q3 y1 W4 g% {/ H
<%@ Register TagPrefix="wmessage" TagName="wname" Src="Welcome.ascx" %>2 K$ h7 u6 O. r+ W/ y0 _
<html>. Y8 c+ e) G: M# l! e
<title>自定义的控件</title>
( v8 p- s8 j* r4 {) m<h3>.NET->Pagelet</h3>$ X1 q! Z0 Y0 ]) z4 R
<wmessage:wname runat="server"/>
% \0 n! g$ `( ]9 x</body>4 o* P. E( @) P/ i/ G( u- C
</html>9 }( F( h3 u( `* {' ^ K
客户端的访问如下:% j, ` T; @9 `6 R' k0 }- m: J
$ [: A- v5 Y/ O3 F2.3.4 组合控件" q9 ~+ W$ h, E6 O7 n/ U
1.定义& T6 V" [; F( M1 J
以类组合形式把已有的控件编译后形成自己定制的控件。实际上组合控件在效果上与利用内置控件形成的用户自定义控件一样,不同处在于,用户自定义控件含有一个.ascx的纯文本控制文件,而组合控件则利用编译后的代码。
3 E+ l2 |1 }' \) h/ ?2.步骤
9 A+ a' c2 k% R" w" V7 M1.)重新定义从Control继承来的CreateChildControls方法。
! r& i9 ]5 l# l: X5 t( b$ A2.)如果组合控件要保持于页面上,须 |