ASP.NET中如何使用 CheckBoxList组件

发布时间:2021-07-16 11:22:24 作者:Leah
来源:亿速云 阅读:131

ASP.NET中如何使用 CheckBoxList组件,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

假定有一个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组件的用法大致也就会了。

一. 如何在创建一个ASP.NET 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组件了。

二. 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).在组件中增加一个检查框,语法如下:

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

(2).访问组件中的检查框,语法如下:

CHKList . Items [ ﹤ index ﹥ ]

(3).删除组件中的检查框,语法如下:

CHKList . Items . Remove ( ﹤ index ﹥ )

四. 实例介绍ASP.NET CheckBoxList组件的使用方法:

(1).如何判定选择了组件中的哪些检查框:

在程序中,是通过处理Selected属性和Count属性来完成的,具体如下:

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

(2).如何设定ASP.NET 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源程序代码如下:

﹤% @ 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 + "  " ;   }  }   }   ﹤/script ﹥   ﹤/head ﹥   ﹤body ﹥   ﹤form runat = "server" ﹥  ﹤h2 align = center ﹥ CheckBoxList组件演示程序 ﹤/h2 ﹥  ﹤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 ﹥

请选择你所需要学习的计算机语言类型:

﹤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 ﹥     ﹤asp:Button Text = "提交" runat = "server" onclick = "Button_Click" /﹥   ﹤h2 ﹥ ﹤font color = red ﹥ 你选择的计算机语言类型为: ﹤/font ﹥ ﹤/h2 ﹥   ﹤asp:Label id = lblResult runat = "server" /﹥   ﹤/form ﹥   ﹤/body ﹥  ﹤/html ﹥

六. ASP.NET CheckBoxList组件编程总结:

其实CheckBoxList组件也是一个服务器端组件。本文介绍了CheckBoxList组件中的一些主要的属性和方法,并且通过一个比较典型的例子说明了在ASP.NET页面中如何进行与CheckBoxList组件相关的编程,其实对于另外一个比较重要的组件--CheckBox来说,他们中有许多的相似之处,掌握了CheckBoxList组件的用法大致也就掌握了CheckBox组件的用法。

看完上述内容,你们掌握ASP.NET中如何使用 CheckBoxList组件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. asp.net 中如何获取CheckBoxList的值
  2. Csharp:asp.net CheckBoxList databind

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net checkboxlist

上一篇:ASP.NET中httpHandler如何使用

下一篇:Web开发中客户端跳转与服务器端跳转有什么区别

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》