ASP.NET CheckBoxList組件中經常使用到的屬性:
I .TextAlign屬性:取值爲:Left、Right。如果TextAlign的值爲Left則CheckBoxList組件中的檢查框的文字在選框的左邊,同理如果TextAlign的值爲Right則檢查框的文字在選框的右邊。
II .Selected屬性:爲布爾型,判定組件中的檢查框是否被選中。
III .RepeatColumns屬性:在CheckBoxList組件中有若干檢查框,此屬性主要是設定這些檢查框到底用多少行來顯示。
IV .RepeatDirection屬性:此屬性的值可爲:Vertical、Horizontal。當設定了RepeatColumns屬性後,設定此屬性是如何排列組件中的各個檢查框的。具體如下:
假定CheckBoxList組件有四個檢查框,並且RepeatColumns屬性值爲2。
(1).如果RepeatDirection = Vertical,則在頁面中檢查框的顯示方式如下:
檢查框01 檢查框03
檢查框02 檢查框04
(2).如果RepeatDirection = Horizontal,則在頁面中檢查框的顯示方式如下:
檢查框01 檢查框02
檢查框03 檢查框04
V .Count屬性:返回CheckBoxList組件中有多少檢查框。
三. ASP.NET CheckBoxList組件編程中經常使用到的方法:
(1).在組件中增加一個檢查框,語法如下:
- CHKList . Items . Add ( new ListItem ( ﹤ text ﹥ , ﹤ value ﹥ ) )
(2).訪問組件中的檢查框,語法如下:
- CHKList . Items [ ﹤ index ﹥ ]
(3).刪除組件中的檢查框,語法如下:
- CHKList . Items . Remove ( ﹤ index ﹥ )
四. 實例介紹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).如何設定ASP.NET CheckBoxList組件的外觀佈局:
CheckBoxList組件有比較多的屬性來設定它的外觀,在本文介紹的程序中,主要是通過四個方面來設定組件的外觀佈局的:組件中的檢查框中的文本和選框的排列位置、組件中各個檢查框佈局、
組件中各個檢查框排列方向和組件中各個檢查框的排列行數,具體的程序代碼如下:
- //組件中的檢查框中的文本和選框的排列位置
- switch ( cboAlign . SelectedIndex )
- {
- case 0 :
- ChkList . TextAlign = TextAlign . Left ;
- break ;
- case 1 :
- ChkList . TextAlign = TextAlign . Right ;
- break ;
- }
- //組件中各個檢查框佈局
- switch ( cboRepeatLayout . SelectedIndex )
- {
- case 0 :
- ChkList . RepeatLayout = RepeatLayout . Table ;
- break ;
- case 1 :
- ChkList . RepeatLayout = RepeatLayout . Flow ;
- break ;
- }
- //組件中各個檢查框排列方向
- switch ( cboRepeatDirection . SelectedIndex)
- {
- case 0 :
- ChkList . RepeatDirection = RepeatDirection . Vertical ;
- break ;
- case 1 :
- ChkList . RepeatDirection = RepeatDirection . Horizontal ;
- break ;
- }
- //組件中各個檢查框的排列行數
- 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 ;
- }
- //組件中各個檢查框佈局
- switch ( cboRepeatLayout . SelectedIndex )
- {
- case 0 :
- ChkList . RepeatLayout = RepeatLayout . Table ;
- break ;
- case 1 :
- ChkList . RepeatLayout = RepeatLayout . Flow ;
- break ;
- }
- //組件中各個檢查框排列方向
- switch ( cboRepeatDirection . SelectedIndex)
- {
- case 0 :
- ChkList . RepeatDirection = RepeatDirection . Vertical ;
- break ;
- case 1 :
- ChkList . RepeatDirection = RepeatDirection . Horizontal ;
- break ;
- }
- //組件中各個檢查框的排列行數
- 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 ﹥ 組件中各個條目佈局: ﹤/td ﹥
- ﹤td ﹥
- ﹤asp:DropDownList id = cboRepeatLayout runat = "server" ﹥
- ﹤asp:ListItem ﹥ 表格型 ﹤/asp:ListItem ﹥
- ﹤asp:ListItem ﹥ 緊湊型 ﹤/asp:ListItem ﹥
- ﹤/asp:DropDownList ﹥
- ﹤/td ﹥
- ﹤/tr ﹥
- ﹤tr ﹥
- ﹤td﹥ 組件中各個條目排列方向:﹤/td ﹥
- ﹤td ﹥
- ﹤asp:DropDownList id = cboRepeatDirection runat = "server" ﹥
- ﹤asp:ListItem ﹥ 水平方向 ﹤/asp:ListItem ﹥
- ﹤asp:ListItem ﹥ 垂直方向 ﹤/asp:ListItem ﹥
- ﹤/asp:DropDownList ﹥
- ﹤/td ﹥
- ﹤/tr ﹥
- ﹤tr ﹥
- ﹤td ﹥ 組件中各個條目排列行數: ﹤/td ﹥
- ﹤td ﹥ ﹤asp:TextBox id = "txtRepeatCols" runat = "server" /﹥ ﹤/td ﹥
- ﹤/tr ﹥
- ﹤/table ﹥
請選擇你所需要學習的計算機語言類型:
- ﹤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 ﹥ 你選擇的計算機語言類型爲: ﹤/font ﹥ ﹤/h1 ﹥
- ﹤asp:Label id = lblResult runat = "server" /﹥
- ﹤/form ﹥
- ﹤/body ﹥
- ﹤/html ﹥