ASP.NET中WebForm組件CheckBoxList編程

來源:互聯網
上載者:User

CheckBox選擇組件是一個程式中都經常的組件。在程式設計中使用到該組件,一般都不會只使用到一個,往往是以多個此類組件的形式出現的。在ASP.NET頁面中如果要使用到多個CheckBox組件,除了添加多個CheckBox組件在頁面中之外,還有一種比較方便的方法,就是使用CheckBoxList組件。CheckBoxList組件是由一組的CheckBox組件組成的,在此組件中CheckBox是做為條目的形式出現的,並且對每個在CheckBoxList組件中的CheckBox都有一個索引號,這樣在程式中就更容易來處理了。

  這時你可能要問,這不是多此一舉麼,既然有了CheckBox組件,還要CheckBoxList組件幹什嗎?這是因為在程式設計的過程中,處理CheckBoxList組件要往往要比處理CheckBox組件相對容易的多並且也明了的多。舉例如下:

  假定有一個CheckBoxList組件和有十個CheckBox組件,並且這個CheckBoxList組件是由這十個CheckBox組件構成的。為了檢測這十個CheckBox組件中的哪些已經被選擇的,如果程式中選用的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組件,在程式設計中的確更明了,更簡潔了。並且只要你掌握了CheckBoxList組 件的用法,CheckBox組件的用法大致也就會了。
一. 如何在建立一個CheckBoxList組件:

<asp:CheckBoxList runat = "server" id = C1 >
<asp:ListItem Value = 1 >第一個檢查框</asp:ListItem >
<asp:ListItem Value = 2 >第二個檢查框</asp:ListItem >
<asp:ListItem Value = 3 >第三個檢查框</asp:ListItem >
.....
注釋:在這裡可以加入若干個檢查框
</asp:CheckBoxList >

  在ASP.NET頁面中加入上面的語句,就可以產生一個名稱為"C1"的CheckBoxList組件了。

  二. 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組件中有多少檢查框。

  三. CheckBoxList組件中經常使用到的方法:

  (1).在組件中增加一個檢查框,文法如下:

   CHKList . Items . Add ( new ListItem ( < text > , < value > ) )

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

   CHKList . Items [ < index > ]

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

   CHKList . Items . Remove ( < index > )
四. 執行個體介紹CheckBoxList組件的使用方法:

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

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

for ( int i = 0 ; i < ChkList . Items . Count ; i++ )
{
if( ChkList . Items [ i ] . Selected )
{
lblResult . Text += ChkList . Items [ i ] .Text + " <br > " ;
}
}

  (2).如何設定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來源程式代碼如下:

<% @ 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 + " <br > " ;
   }
  }
 }
 </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 >
  <br >
   請選擇你所需要學習的電腦語言類型:
  <br >
  <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 >
  <br >
   <asp:Button Text = "提交" runat = "server" onclick = "Button_Click" />
   <h1 > <font color = red > 你選擇的電腦語言類型為: </font > </h1 >
   <asp:Label id = lblResult runat = "server" />
 </form >
 </body >
</html >
 

  六. 總結:

  其實CheckBoxList組件也是一個伺服器端組件。本文介紹了CheckBoxList組件中的一些主要的屬性和方法,並且通過一個比較典型的例子說明了在ASP.NET頁面中如何進行與CheckBoxList組件相關的編程,其實對於另外一個比較重要的組件--CheckBox來說,他們中有許多的相似之處,掌握了CheckBoxList組件的用法大致也就掌握了CheckBox組件的用法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.