日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

ASP.NET中WebForm组件CheckBoxList编程

發布時間:2023/11/27 生活经验 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET中WebForm组件CheckBoxList编程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

作者:馬金虎? 來自:yesky

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)和執行的界面

  下圖是執行了下列源程序代碼(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 + " <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組件的用法。

總結

以上是生活随笔為你收集整理的ASP.NET中WebForm组件CheckBoxList编程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。