[.NET]CheckBoxList 用法

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).在組件中增加一個檢查框,語法如下:

  1. CHKList . Items . Add ( new ListItem ( ﹤ text ﹥ , ﹤ value ﹥ ) )

(2).訪問組件中的檢查框,語法如下:

  1. CHKList . Items [ ﹤ index ﹥ ]

(3).刪除組件中的檢查框,語法如下:

  1. CHKList . Items . Remove ( ﹤ index ﹥ )

四. 實例介紹ASP.NET CheckBoxList組件的使用方法:

(1).如何判定選擇了組件中的哪些檢查框:

在程序中,是通過處理Selected屬性和Count屬性來完成的,具體如下:

  1. for ( int i = 0 ; i ﹤ ChkList . Items . Count ; i++ ) 
  2. if( ChkList . Items [ i ] . Selected ) 
  3. lblResult . Text += ChkList . Items [ i ] .Text + "  " ; 
  4. }

(2).如何設定ASP.NET CheckBoxList組件的外觀佈局:

CheckBoxList組件有比較多的屬性來設定它的外觀,在本文介紹的程序中,主要是通過四個方面來設定組件的外觀佈局的:組件中的檢查框中的文本和選框的排列位置、組件中各個檢查框佈局、

組件中各個檢查框排列方向和組件中各個檢查框的排列行數,具體的程序代碼如下:

  1. //組件中的檢查框中的文本和選框的排列位置
  2. switch ( cboAlign . SelectedIndex ) 
  3.  case 0 : 
  4. ChkList . TextAlign = TextAlign . Left ; 
  5. break
  6.  case 1 : 
  7. ChkList . TextAlign = TextAlign . Right ; 
  8. break
  9. //組件中各個檢查框佈局
  10. switch ( cboRepeatLayout . SelectedIndex ) 
  11.  case 0 : 
  12. ChkList . RepeatLayout = RepeatLayout . Table ; 
  13. break
  14.  case 1 : 
  15. ChkList . RepeatLayout = RepeatLayout . Flow ; 
  16. break
  17. //組件中各個檢查框排列方向
  18. switch ( cboRepeatDirection . SelectedIndex) 
  19.  case 0 : 
  20. ChkList . RepeatDirection = RepeatDirection . Vertical ; 
  21. break
  22.  case 1 : 
  23. ChkList . RepeatDirection = RepeatDirection . Horizontal ; 
  24. break
  25. //組件中各個檢查框的排列行數
  26. try
  27.  int cols = int . Parse ( txtRepeatCols.Text ) ; 
  28.  ChkList . RepeatColumns = cols ; 
  29. catch ( Exception ) 
  30. }

五. 文中源程序代碼(Check.aspx):

Check.aspx源程序代碼如下:

  1. ﹤% @ Page Language = "C#" %﹥ 
  2. ﹤html ﹥ 
  3. ﹤head ﹥ 
  4. ﹤title ﹥ CheckBoxList組件演示程序 ﹤/title ﹥ 
  5. ﹤script runat = "server" ﹥ 
  6.  protected void Button_Click ( object sender , EventArgs e ) 
  7.  { 
  8. //組件中的檢查框中的文本和選框的排列位置
  9. switch ( cboAlign . SelectedIndex ) 
  10.  case 0 : 
  11. ChkList . TextAlign = TextAlign . Left ; 
  12. break
  13.  case 1 : 
  14. ChkList . TextAlign = TextAlign . Right ; 
  15. break
  16. //組件中各個檢查框佈局
  17. switch ( cboRepeatLayout . SelectedIndex ) 
  18.  case 0 : 
  19. ChkList . RepeatLayout = RepeatLayout . Table ; 
  20. break
  21.  case 1 : 
  22. ChkList . RepeatLayout = RepeatLayout . Flow ; 
  23. break
  24. //組件中各個檢查框排列方向
  25. switch ( cboRepeatDirection . SelectedIndex) 
  26.  case 0 : 
  27. ChkList . RepeatDirection = RepeatDirection . Vertical ; 
  28. break
  29.  case 1 : 
  30. ChkList . RepeatDirection = RepeatDirection . Horizontal ; 
  31. break
  32. //組件中各個檢查框的排列行數
  33. try
  34.  int cols = int . Parse ( txtRepeatCols.Text ) ; 
  35.  ChkList . RepeatColumns = cols ; 
  36. catch ( Exception ) 
  37. lblResult . Text = "" ; 
  38. for ( int i = 0 ; i ﹤ ChkList . Items . Count ; i++ ) 
  39.  if( ChkList . Items [ i ] . Selected ) 
  40.  { 
  41. lblResult . Text += ChkList . Items [ i ] .Text + "  " ; 
  42.  } 
  43.  } 
  44.  ﹤/script ﹥ 
  45.  ﹤/head ﹥ 
  46.  ﹤body ﹥ 
  47.  ﹤form runat = "server" ﹥ 
  48. ﹤h1 align = center ﹥ CheckBoxList組件演示程序 ﹤/h1 ﹥ 
  49. ﹤table ﹥ 
  50.  ﹤tr ﹥ 
  51. ﹤td ﹥ 組件中的文本排列位置: ﹤/td ﹥ 
  52. ﹤td ﹥ 
  53. ﹤asp:DropDownList id = cboAlign runat = "server" ﹥ 
  54.  ﹤asp:ListItem ﹥ 居左 ﹤/asp:ListItem ﹥ 
  55.  ﹤asp:ListItem ﹥ 居右 ﹤/asp:ListItem ﹥ 
  56. ﹤/asp:DropDownList ﹥ 
  57. ﹤/td ﹥ 
  58.  ﹤/tr ﹥ 
  59.  ﹤tr ﹥ 
  60. ﹤td ﹥ 組件中各個條目佈局: ﹤/td ﹥ 
  61. ﹤td ﹥ 
  62. ﹤asp:DropDownList id = cboRepeatLayout runat = "server" ﹥ 
  63.  ﹤asp:ListItem ﹥ 表格型 ﹤/asp:ListItem ﹥ 
  64.  ﹤asp:ListItem ﹥ 緊湊型 ﹤/asp:ListItem ﹥ 
  65. ﹤/asp:DropDownList ﹥ 
  66. ﹤/td ﹥ 
  67.  ﹤/tr ﹥ 
  68.  ﹤tr ﹥ 
  69. ﹤td﹥ 組件中各個條目排列方向:﹤/td ﹥ 
  70. ﹤td ﹥ 
  71. ﹤asp:DropDownList id = cboRepeatDirection runat = "server" ﹥ 
  72.  ﹤asp:ListItem ﹥ 水平方向 ﹤/asp:ListItem ﹥ 
  73.  ﹤asp:ListItem ﹥ 垂直方向 ﹤/asp:ListItem ﹥ 
  74. ﹤/asp:DropDownList ﹥ 
  75. ﹤/td ﹥ 
  76.  ﹤/tr ﹥ 
  77.  ﹤tr ﹥ 
  78. ﹤td ﹥ 組件中各個條目排列行數: ﹤/td ﹥ 
  79. ﹤td ﹥ ﹤asp:TextBox id = "txtRepeatCols" runat = "server" /﹥ ﹤/td ﹥ 
  80.  ﹤/tr ﹥ 
  81. ﹤/table ﹥

請選擇你所需要學習的計算機語言類型:

  1. ﹤asp:CheckBoxList id = "ChkList" RepeatDirection = Horizontal runat = "server" ﹥ 
  2.  ﹤asp:ListItem ﹥ Visual C++ .Net ﹤/asp:ListItem ﹥ 
  3.  ﹤asp:ListItem ﹥ Visual C# ﹤/asp:ListItem ﹥ 
  4.  ﹤asp:ListItem ﹥ VB.NET ﹤/asp:ListItem ﹥ 
  5.  ﹤asp:ListItem ﹥ JScript.NET ﹤/asp:ListItem ﹥ 
  6.  ﹤asp:ListItem ﹥ Visual J# ﹤/asp:ListItem ﹥ 
  7. ﹤/asp:CheckBoxList ﹥ 
  8.  ﹤asp:Button Text = "提交" runat = "server" onclick = "Button_Click" /﹥ 
  9.  ﹤h1 ﹥ ﹤font color = red ﹥ 你選擇的計算機語言類型爲: ﹤/font ﹥ ﹤/h1 ﹥ 
  10.  ﹤asp:Label id = lblResult runat = "server" /﹥ 
  11.  ﹤/form ﹥ 
  12.  ﹤/body ﹥ 
  13. ﹤/html ﹥
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章