分享

CheckBoxList - 盼君的日志 - 网易博客

 zhang_shao 2010-11-17

CheckBoxList控件用来建立一个多选的复选框组。CheckBoxList控件中的每个可选项由一个ListItem元素来定义!提示:此控件支持数据绑定!
如果当我们要使用一群的CheckBox Web控件时,在程序的判断上非常麻烦,因此CheckBoxList Web 控件和RadioButtonList Web 控件一样是让我们方便的取得用户选取的项目。如果将复选框绑定到数据源,则不能使用CheckBox控件。

一、CheckBox控件属性和事件
1、AutoPostBack属性:用于设置当单击checkboxList控件时,是否自动回送到服务器。True表示回送;False(默认)表示不回送。
2、DataSource属性:用于指定填充列表控件的数据源。
3、DataTextField属性:指定DataSource中一个字段,该字段的值对应于列表项的Text属性。
4、DataValueField属性:指定DataSource中一个字段,字段的值对应于列表项的Value属性。
5、Items属性:表示复选框列表中各个选项的集合,如CheckBoxList1.Items(i)表示第i个选项,i从0开始。每个选项都有以下3个基本属性:
Text属性:表示每个选项的文本。
Value属性:表示每个选项的选项值。
Selected属性:表示该选项是否被选中。
Count属性:通过Items.Count属性可获得CheckBoxList控件的选项数;
Add方法:通过items.Add方法可以向CheckBoxList控件添加选项;
Remove方法:通过items.Remove方法,可从CheckBoxList控件中删除指定的选项;
Insert方法:通过items.insert方法,可将一个新的选项插入到CheckBoxList控件中;
Clear方法:通过items.clear方法可以清空CheckBoxList控件中的选项。
6、RepeatColumns属性:用于指定在CheckBoxList控件中显示选项占用几列。默认值为0,表示任意多列。
7、RepeatDirection属性:用于指定CheckBoxList控件的显示方向。Vertical时,列表项以列优先排列的形式显示;Horizontal时,列项以行优先排列的形式显示。
8、RepeatLayout属性:用于设置选项的排列方式。Table(默认)时,以表结构显示,属性值为Flow时,不以表结构显示。
9、SelectedIndex属性:用于获取或设置列表中选定项的最低序号索引值。如果列表控件中只有一个选项被选中,则该属性表示当前选定项的索引值。
10、SelectedItem属性:用于获取列表控件中索引值最小的选定项。如果列表中只有一个选项被选中,则该属性表示当前选定项。通过该属性可获得选定项的Text和Value属性值。
11:SelectedIndexchanged事件:当用户选择了列表中的任意复选框时,都将引发事件。

二、使用语法

<ASP:CheckBoxList

  Id="控件名称"

  Runat="Server"

  AutoPostBack="True | False"

  CellPadding="像素"

  DataSource="<%数据源%>"

  DataTextField="数据源的字段"

  DataValueField="数据源的字段"

  RepeatColumns="字段数量"

  RepeatDirection="Vertical | Horizontal"

  RepeatLayout="Flow | Table"

TextAlign="Right | Left"

  OnSelectedIndexChanged="事件程序名称"

>

  <ASP:ListItem/>

</ASP:CheckBoxList>

三、使用实例
<Script Language="C#" Runat="Server">
public void Sub_Click(Object src,EventArgs e)
{
 string Resulte = null;
 for(int i=0;i<chkList.Items.Count;i++)
 {
  if(chkList.Items[i].Selected)
  {
   Resulte = Resulte + "CheckBox"+i.ToString()+"已经选中<br>";
  }
 }
 lblShow.Text = Resulte;
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<b>CheckBoxList控件演示</b>
<hr>
<form runat="server">
<asp:CheckBoxList id="chkList"  runat="server" >
 <asp:ListItem>CheckBox0</asp:ListItem>
 <asp:ListItem>CheckBox1</asp:ListItem>
 <asp:ListItem>CheckBox2</asp:ListItem>
 <asp:ListItem>CheckBox3</asp:ListItem>
 <asp:ListItem>CheckBox4</asp:ListItem>
</asp:CheckBoxList>
<asp:Button Text="提交" OnClick="Sub_Click" runat="server" />
<hr>
<asp:Label id="lblShow" runat="server" />

</form>
</body>
</html>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约