自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET CheckBoxList組件編程淺析

開發(fā) 后端
ASP.NET CheckBoxList組件編程主要想大家講述ASP.NET CheckBoxList的常用屬性使用和各自的特點(diǎn)以及學(xué)習(xí)的總結(jié)。

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組件就需要如下代碼:

  1.  if ( C1 . Checked )  
  2.  {  
  3.   }  
  4.  if ( C2 . Checked )  
  5.  {  
  6.   }  
  7.  ....  
  8.  if ( C10 . Checked )  
  9.  {  
  10.  }  

但如果程序中使用了CheckBoxList組件,就只需要以下這幾行代碼就可以了:

  1.  for ( int i = 0 ; i ﹤ CHK . Items . Count ; i++ )  
  2.  {  
  3.   if ( CHK . Items [ i ] . Selected )  
  4.   {  
  5.    //處理你要完成的工作  
  6.   }  
  7.  } 

注釋:其中C1 -- C10是CheckBox組件,CHK是CheckBoxList組件

可見用了CheckBoxList組件,在程序設(shè)計(jì)中的確更明了,更簡潔了。并且只要你掌握了CheckBoxList組 件的用法,CheckBox組件的用法大致也就會了。

一. 如何在創(chuàng)建一個(gè)ASP.NET CheckBoxList組件:

  1. ﹤asp:CheckBoxList runat = "server" id = C1 ﹥  
  2. ﹤asp:ListItem Value = 1 ﹥***個(gè)檢查框﹤/asp:ListItem ﹥  
  3. ﹤asp:ListItem Value = 2 ﹥第二個(gè)檢查框﹤/asp:ListItem ﹥  
  4. ﹤asp:ListItem Value = 3 ﹥第三個(gè)檢查框﹤/asp:ListItem ﹥  
  5. .....  
  6.  
  7. //注釋:在這里可以加入若干個(gè)檢查框  
  8.  
  9. ﹤/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è)檢查框,語法如下:

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

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

  1. CHKList . Items [ ﹤ index ﹥ ] 

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

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

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

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

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

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

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

CheckBoxList組件有比較多的屬性來設(shè)定它的外觀,在本文介紹的程序中,主要是通過四個(gè)方面來設(shè)定組件的外觀布局的:組件中的檢查框中的文本和選框的排列位置、組件中各個(gè)檢查框布局、組件中各個(gè)檢查框排列方向和組件中各個(gè)檢查框的排列行數(shù),具體的程序代碼如下:

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

五. 文中源程序代碼(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.   {  
  11.    case 0 :  
  12.     ChkList . TextAlign = TextAlign . Left ;  
  13.     break ;  
  14.    case 1 :  
  15.     ChkList . TextAlign = TextAlign . Right ;  
  16.     break ;  
  17.   }  
  18.   //組件中各個(gè)檢查框布局  
  19.   switch ( cboRepeatLayout . SelectedIndex )  
  20.   {  
  21.    case 0 :  
  22.     ChkList . RepeatLayout = RepeatLayout . Table ;  
  23.     break ;  
  24.    case 1 :  
  25.     ChkList . RepeatLayout = RepeatLayout . Flow ;  
  26.     break ;  
  27.   }  
  28.   //組件中各個(gè)檢查框排列方向  
  29.   switch ( cboRepeatDirection . SelectedIndex)  
  30.   {  
  31.    case 0 :  
  32.     ChkList . RepeatDirection = RepeatDirection . Vertical ;  
  33.     break ;  
  34.    case 1 :  
  35.     ChkList . RepeatDirection = RepeatDirection . Horizontal ;  
  36.     break ;  
  37.   }  
  38.   //組件中各個(gè)檢查框的排列行數(shù)  
  39.   try 
  40.   {  
  41.    int cols = int . Parse ( txtRepeatCols.Text ) ;  
  42.    ChkList . RepeatColumns = cols ;  
  43.   }  
  44.   catch ( Exception )  
  45.   {  
  46.   }  
  47.   lblResult . Text = "" ;  
  48.   for ( int i = 0 ; i ﹤ ChkList . Items . Count ; i++ )  
  49.   {  
  50.    if( ChkList . Items [ i ] . Selected )  
  51.    {  
  52.     lblResult . Text += ChkList . Items [ i ] .Text + "  " ;  
  53.    }  
  54.   }  
  55.  }  
  56.  ﹤/script ﹥  
  57.  ﹤/head ﹥  
  58.  ﹤body ﹥  
  59.  ﹤form runat = "server" ﹥  
  60.   ﹤h1 align = center ﹥ CheckBoxList組件演示程序 ﹤/h1 ﹥  
  61.   ﹤table ﹥  
  62.    ﹤tr ﹥  
  63.     ﹤td ﹥ 組件中的文本排列位置: ﹤/td ﹥  
  64.     ﹤td ﹥  
  65.       ﹤asp:DropDownList id = cboAlign runat = "server" ﹥  
  66.        ﹤asp:ListItem ﹥ 居左 ﹤/asp:ListItem ﹥  
  67.        ﹤asp:ListItem ﹥ 居右 ﹤/asp:ListItem ﹥  
  68.       ﹤/asp:DropDownList ﹥  
  69.     ﹤/td ﹥  
  70.    ﹤/tr ﹥  
  71.    ﹤tr ﹥  
  72.     ﹤td ﹥ 組件中各個(gè)條目布局: ﹤/td ﹥  
  73.     ﹤td ﹥  
  74.       ﹤asp:DropDownList id = cboRepeatLayout runat = "server" ﹥  
  75.        ﹤asp:ListItem ﹥ 表格型 ﹤/asp:ListItem ﹥  
  76.        ﹤asp:ListItem ﹥ 緊湊型 ﹤/asp:ListItem ﹥  
  77.       ﹤/asp:DropDownList ﹥  
  78.     ﹤/td ﹥  
  79.    ﹤/tr ﹥  
  80.    ﹤tr ﹥  
  81.     ﹤td﹥ 組件中各個(gè)條目排列方向:﹤/td ﹥  
  82.     ﹤td ﹥  
  83.       ﹤asp:DropDownList id = cboRepeatDirection runat = "server" ﹥  
  84.        ﹤asp:ListItem ﹥ 水平方向 ﹤/asp:ListItem ﹥  
  85.        ﹤asp:ListItem ﹥ 垂直方向 ﹤/asp:ListItem ﹥  
  86.       ﹤/asp:DropDownList ﹥  
  87.     ﹤/td ﹥  
  88.    ﹤/tr ﹥  
  89.    ﹤tr ﹥  
  90.     ﹤td ﹥ 組件中各個(gè)條目排列行數(shù): ﹤/td ﹥  
  91.     ﹤td ﹥ ﹤asp:TextBox id = "txtRepeatCols" runat = "server" /﹥ ﹤/td ﹥  
  92.    ﹤/tr ﹥  
  93.   ﹤/table ﹥ 

請選擇你所需要學(xué)習(xí)的計(jì)算機(jī)語言類型:

  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.     
  9.    ﹤asp:Button Text = "提交" runat = "server" onclick = "Button_Click" /﹥  
  10.    ﹤h1 ﹥ ﹤font color = red ﹥ 你選擇的計(jì)算機(jī)語言類型為: ﹤/font ﹥ ﹤/h1 ﹥  
  11.    ﹤asp:Label id = lblResult runat = "server" /﹥  
  12.  ﹤/form ﹥  
  13.  ﹤/body ﹥  
  14. ﹤/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組件編程有所幫助。

【編輯推薦】

  1. ASP.NET組件設(shè)計(jì)之生命周期詳解
  2. ASP.NET組件設(shè)計(jì)之傳輸機(jī)制淺析
  3. ASP.NET組件設(shè)計(jì)之復(fù)雜屬性和狀態(tài)管理淺析
  4. ASP.NET httpHandler使用淺析
  5. ASP.NET組件編程之事件編寫淺析
責(zé)任編輯:仲衡 來源: 網(wǎng)站愛好者協(xié)會
相關(guān)推薦

2009-08-05 18:36:12

ASP.NET Che

2009-07-31 13:06:53

CheckBoxLisASP.NET頁面

2009-08-10 16:07:44

ASP.NET Lin

2009-07-24 18:02:46

ASP.NET編程

2009-08-05 16:53:14

ASP.NET組件設(shè)計(jì)

2009-08-10 13:32:15

ASP.NET TimASP.NET組件設(shè)計(jì)

2009-08-03 13:38:18

ASP.NET編程模型

2009-08-10 15:26:46

ASP.NET組件編程

2009-08-03 13:12:34

ASP.NET編程模型

2009-07-27 13:34:15

ASP.NET編程

2009-08-05 16:59:55

ASP.NET組件設(shè)計(jì)

2009-07-24 13:41:15

ASP.NET AJA

2009-08-03 18:00:00

ASP.NET服務(wù)器控

2009-08-03 11:21:47

ASP.NET編程模型

2009-07-31 12:43:59

ASP.NET MVC

2009-08-05 15:50:13

ASP.NET優(yōu)點(diǎn)

2009-08-03 17:07:13

ASP.NET編程規(guī)范

2009-08-03 16:57:42

ASP.NET編程規(guī)范

2009-08-10 14:38:29

ASP.NET組件設(shè)計(jì)

2009-07-27 12:56:27

控件CheckBoxLASP.NET服務(wù)器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號