打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[.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 ﹥ 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
CheckBoxList - 盼君的日志 - 网易博客
DataList与Repeater
关于Flutter的RichText组件你了解吗?
怎样防止电脑IP地址被盗(图解)
Flutter学习笔记(11)--文本组件、图标及按钮组件
Flutter学习笔记(21)--TextField文本框组件和Card卡片组件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服