ASP.NET CheckBoxList組件編程淺析
ASP.NET CheckBoxList組件編程的出現(xiàn):CheckBox選擇組件是一個(gè)程序中都經(jīng)常的組件。在程序設(shè)計(jì)中使用到該組件,一般都不會只使用到一個(gè),往往是以多個(gè)此類組件的形式出現(xiàn)的。在ASP.NET頁面中如果要使用到多個(gè)CheckBox組件,除了添加多個(gè)CheckBox組件在頁面中之外,還有一種比較方便的方法,就是使用CheckBoxList組件。CheckBoxList組件是由一組的CheckBox組件組成的,在此組件中CheckBox是做為條目的形式出現(xiàn)的,并且對每個(gè)在CheckBoxList組件中的CheckBox都有一個(gè)索引號,這樣在程序中就更容易來處理了。
這時(shí)你可能要問,這不是多此一舉么,既然有了CheckBox組件,還要CheckBoxList組件干什么?這是因?yàn)樵诔绦蛟O(shè)計(jì)的過程中,處理CheckBoxList組件要往往要比處理CheckBox組件相對容易的多并且也明了的多。舉例如下:
假定有一個(gè)CheckBoxList組件和有十個(gè)CheckBox組件,并且這個(gè)CheckBoxList組件是由這十個(gè)CheckBox組件構(gòu)成的。為了檢測這十個(gè)CheckBox組件中的哪些已經(jīng)被選擇的,如果程序中選用的CheckBox組件就需要如下代碼:
- if ( C1 . Checked )
- {
- }
- if ( C2 . Checked )
- {
- }
- ....
- if ( C10 . Checked )
- {
- }
但如果程序中使用了CheckBoxList組件,就只需要以下這幾行代碼就可以了:
- for ( int i = 0 ; i ﹤ CHK . Items . Count ; i++ )
- {
- if ( CHK . Items [ i ] . Selected )
- {
- //處理你要完成的工作
- }
- }
注釋:其中C1 -- C10是CheckBox組件,CHK是CheckBoxList組件
可見用了CheckBoxList組件,在程序設(shè)計(jì)中的確更明了,更簡潔了。并且只要你掌握了CheckBoxList組 件的用法,CheckBox組件的用法大致也就會了。
一. 如何在創(chuàng)建一個(gè)ASP.NET CheckBoxList組件:
- ﹤asp:CheckBoxList runat = "server" id = C1 ﹥
- ﹤asp:ListItem Value = 1 ﹥***個(gè)檢查框﹤/asp:ListItem ﹥
- ﹤asp:ListItem Value = 2 ﹥第二個(gè)檢查框﹤/asp:ListItem ﹥
- ﹤asp:ListItem Value = 3 ﹥第三個(gè)檢查框﹤/asp:ListItem ﹥
- .....
- //注釋:在這里可以加入若干個(gè)檢查框
- ﹤/asp:CheckBoxList ﹥
在ASP.NET頁面中加入上面的語句,就可以產(chǎn)生一個(gè)名稱為"C1"的CheckBoxList組件了。
二. ASP.NET CheckBoxList組件中經(jīng)常使用到的屬性:
I .TextAlign屬性:取值為:Left、Right。如果TextAlign的值為Left則CheckBoxList組件中的檢查框的文字在選框的左邊,同理如果TextAlign的值為Right則檢查框的文字在選框的右邊。
II .Selected屬性:為布爾型,判定組件中的檢查框是否被選中。
III .RepeatColumns屬性:在CheckBoxList組件中有若干檢查框,此屬性主要是設(shè)定這些檢查框到底用多少行來顯示。
IV .RepeatDirection屬性:此屬性的值可為:Vertical、Horizontal。當(dāng)設(shè)定了RepeatColumns屬性后,設(shè)定此屬性是如何排列組件中的各個(gè)檢查框的。具體如下:
假定CheckBoxList組件有四個(gè)檢查框,并且RepeatColumns屬性值為2。
(1).如果RepeatDirection = Vertical,則在頁面中檢查框的顯示方式如下:
檢查框01 檢查框03
檢查框02 檢查框04
(2).如果RepeatDirection = Horizontal,則在頁面中檢查框的顯示方式如下:
檢查框01 檢查框02
檢查框03 檢查框04
V .Count屬性:返回CheckBoxList組件中有多少檢查框。
三. ASP.NET CheckBoxList組件編程中經(jīng)常使用到的方法:
(1).在組件中增加一個(gè)檢查框,語法如下:
- CHKList . Items . Add ( new ListItem ( ﹤ text ﹥ , ﹤ value ﹥ ) )
(2).訪問組件中的檢查框,語法如下:
- CHKList . Items [ ﹤ index ﹥ ]
(3).刪除組件中的檢查框,語法如下:
- CHKList . Items . Remove ( ﹤ index ﹥ )
四. 實(shí)例介紹ASP.NET CheckBoxList組件的使用方法:
(1).如何判定選擇了組件中的哪些檢查框:
在程序中,是通過處理Selected屬性和Count屬性來完成的,具體如下:
- for ( int i = 0 ; i ﹤ ChkList . Items . Count ; i++ )
- {
- if( ChkList . Items [ i ] . Selected )
- {
- lblResult . Text += ChkList . Items [ i ] .Text + " " ;
- }
- }
(2).如何設(shè)定ASP.NET CheckBoxList組件的外觀布局:
CheckBoxList組件有比較多的屬性來設(shè)定它的外觀,在本文介紹的程序中,主要是通過四個(gè)方面來設(shè)定組件的外觀布局的:組件中的檢查框中的文本和選框的排列位置、組件中各個(gè)檢查框布局、組件中各個(gè)檢查框排列方向和組件中各個(gè)檢查框的排列行數(shù),具體的程序代碼如下:
- //組件中的檢查框中的文本和選框的排列位置
- switch ( cboAlign . SelectedIndex )
- {
- case 0 :
- ChkList . TextAlign = TextAlign . Left ;
- break ;
- case 1 :
- ChkList . TextAlign = TextAlign . Right ;
- break ;
- }
- //組件中各個(gè)檢查框布局
- switch ( cboRepeatLayout . SelectedIndex )
- {
- case 0 :
- ChkList . RepeatLayout = RepeatLayout . Table ;
- break ;
- case 1 :
- ChkList . RepeatLayout = RepeatLayout . Flow ;
- break ;
- }
- //組件中各個(gè)檢查框排列方向
- switch ( cboRepeatDirection . SelectedIndex)
- {
- case 0 :
- ChkList . RepeatDirection = RepeatDirection . Vertical ;
- break ;
- case 1 :
- ChkList . RepeatDirection = RepeatDirection . Horizontal ;
- break ;
- }
- //組件中各個(gè)檢查框的排列行數(shù)
- try
- {
- int cols = int . Parse ( txtRepeatCols.Text ) ;
- ChkList . RepeatColumns = cols ;
- }
- catch ( Exception )
- {
- }
五. 文中源程序代碼(Check.aspx):
Check.aspx源程序代碼如下:
- ﹤% @ Page Language = "C#" %﹥
- ﹤html ﹥
- ﹤head ﹥
- ﹤title ﹥ CheckBoxList組件演示程序 ﹤/title ﹥
- ﹤script runat = "server" ﹥
- protected void Button_Click ( object sender , EventArgs e )
- {
- //組件中的檢查框中的文本和選框的排列位置
- switch ( cboAlign . SelectedIndex )
- {
- case 0 :
- ChkList . TextAlign = TextAlign . Left ;
- break ;
- case 1 :
- ChkList . TextAlign = TextAlign . Right ;
- break ;
- }
- //組件中各個(gè)檢查框布局
- switch ( cboRepeatLayout . SelectedIndex )
- {
- case 0 :
- ChkList . RepeatLayout = RepeatLayout . Table ;
- break ;
- case 1 :
- ChkList . RepeatLayout = RepeatLayout . Flow ;
- break ;
- }
- //組件中各個(gè)檢查框排列方向
- switch ( cboRepeatDirection . SelectedIndex)
- {
- case 0 :
- ChkList . RepeatDirection = RepeatDirection . Vertical ;
- break ;
- case 1 :
- ChkList . RepeatDirection = RepeatDirection . Horizontal ;
- break ;
- }
- //組件中各個(gè)檢查框的排列行數(shù)
- try
- {
- int cols = int . Parse ( txtRepeatCols.Text ) ;
- ChkList . RepeatColumns = cols ;
- }
- catch ( Exception )
- {
- }
- lblResult . Text = "" ;
- for ( int i = 0 ; i ﹤ ChkList . Items . Count ; i++ )
- {
- if( ChkList . Items [ i ] . Selected )
- {
- lblResult . Text += ChkList . Items [ i ] .Text + " " ;
- }
- }
- }
- ﹤/script ﹥
- ﹤/head ﹥
- ﹤body ﹥
- ﹤form runat = "server" ﹥
- ﹤h1 align = center ﹥ CheckBoxList組件演示程序 ﹤/h1 ﹥
- ﹤table ﹥
- ﹤tr ﹥
- ﹤td ﹥ 組件中的文本排列位置: ﹤/td ﹥
- ﹤td ﹥
- ﹤asp:DropDownList id = cboAlign runat = "server" ﹥
- ﹤asp:ListItem ﹥ 居左 ﹤/asp:ListItem ﹥
- ﹤asp:ListItem ﹥ 居右 ﹤/asp:ListItem ﹥
- ﹤/asp:DropDownList ﹥
- ﹤/td ﹥
- ﹤/tr ﹥
- ﹤tr ﹥
- ﹤td ﹥ 組件中各個(gè)條目布局: ﹤/td ﹥
- ﹤td ﹥
- ﹤asp:DropDownList id = cboRepeatLayout runat = "server" ﹥
- ﹤asp:ListItem ﹥ 表格型 ﹤/asp:ListItem ﹥
- ﹤asp:ListItem ﹥ 緊湊型 ﹤/asp:ListItem ﹥
- ﹤/asp:DropDownList ﹥
- ﹤/td ﹥
- ﹤/tr ﹥
- ﹤tr ﹥
- ﹤td﹥ 組件中各個(gè)條目排列方向:﹤/td ﹥
- ﹤td ﹥
- ﹤asp:DropDownList id = cboRepeatDirection runat = "server" ﹥
- ﹤asp:ListItem ﹥ 水平方向 ﹤/asp:ListItem ﹥
- ﹤asp:ListItem ﹥ 垂直方向 ﹤/asp:ListItem ﹥
- ﹤/asp:DropDownList ﹥
- ﹤/td ﹥
- ﹤/tr ﹥
- ﹤tr ﹥
- ﹤td ﹥ 組件中各個(gè)條目排列行數(shù): ﹤/td ﹥
- ﹤td ﹥ ﹤asp:TextBox id = "txtRepeatCols" runat = "server" /﹥ ﹤/td ﹥
- ﹤/tr ﹥
- ﹤/table ﹥
請選擇你所需要學(xué)習(xí)的計(jì)算機(jī)語言類型:
- ﹤asp:CheckBoxList id = "ChkList" RepeatDirection = Horizontal runat = "server" ﹥
- ﹤asp:ListItem ﹥ Visual C++ .Net ﹤/asp:ListItem ﹥
- ﹤asp:ListItem ﹥ Visual C# ﹤/asp:ListItem ﹥
- ﹤asp:ListItem ﹥ VB.NET ﹤/asp:ListItem ﹥
- ﹤asp:ListItem ﹥ JScript.NET ﹤/asp:ListItem ﹥
- ﹤asp:ListItem ﹥ Visual J# ﹤/asp:ListItem ﹥
- ﹤/asp:CheckBoxList ﹥
- ﹤asp:Button Text = "提交" runat = "server" onclick = "Button_Click" /﹥
- ﹤h1 ﹥ ﹤font color = red ﹥ 你選擇的計(jì)算機(jī)語言類型為: ﹤/font ﹥ ﹤/h1 ﹥
- ﹤asp:Label id = lblResult runat = "server" /﹥
- ﹤/form ﹥
- ﹤/body ﹥
- ﹤/html ﹥
六. ASP.NET CheckBoxList組件編程總結(jié):
其實(shí)CheckBoxList組件也是一個(gè)服務(wù)器端組件。本文介紹了CheckBoxList組件中的一些主要的屬性和方法,并且通過一個(gè)比較典型的例子說明了在ASP.NET頁面中如何進(jìn)行與CheckBoxList組件相關(guān)的編程,其實(shí)對于另外一個(gè)比較重要的組件--CheckBox來說,他們中有許多的相似之處,掌握了CheckBoxList組件的用法大致也就掌握了CheckBox組件的用法。
ASP.NET CheckBoxList組件編程的相關(guān)內(nèi)容就向你介紹到這里,希望對你學(xué)習(xí)ASP.NET CheckBoxList組件編程有所幫助。
【編輯推薦】