CheckBoxList

如何遍历CheckBoxList,获得选中项的值?动态绑定CheckBoxList?
 

CheckBoxList,相信大家应该并不陌生,特别是在投票里面,还有爱好啦什么的里面很有有用,下面我们就来探讨一下CheckBoxList的基本用法:

1.CheckBoxList的动态绑定

基本思路如下:

在Page_load里,加载页面的时候从数据库里读取Northwind数据库里表Employees表的值绑定到CheckBoxlist上.这样就把数据库的表的内容绑定好了.

if(!this.IsPostBack)
            
{
                 SqlConnection con
=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=;");
                 con.Open();
                 SqlCommand cmd
=new SqlCommand("select top 10 * from Employees",con);
                 SqlDataReader sdr
=cmd.ExecuteReader();
                
try
                
{
                    
this.chklistA.DataSource=sdr;
                    
this.chklistA.DataTextField="LastName";
                    
this.chklistA.DataValueField="EmployeeID";
                    
this.chklistA.DataBind();
                 }

                
catch(Exception Ex)
                
{
                    
throw Ex;
                 }

                
finally
                
{
                     sdr.Close();
                     con.Close();
                     con.Dispose();
                 }

             }

2.添加一个按钮button1,在button1的Button1_Click事件里插入一下代码,这就是如何遍历ch

foreach(ListItem li inthis.chklistA.Items)
            
{
                
if(li.Selected==true)
                
{
                     Response.Write(li.Text
+"<br>");
                 }

             }

3.选中CheckBox,但li.Selected=false

在page_load中的加载数据的时候没有放在IsPostBack中,应为:

if   (!IsPostBack)
{  
        ..............         checkboxlist的数据绑定、加载
}

            

 

dropdownlist绑定增加“全部”选项

  ddlOrgan.Items.Insert(0, new ListItem("-----全部-----", "All"));

 

 

 

CheckBoxList 详细说明

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>

posted @ 2011-10-27 12:31  huangqing  阅读(10217)  评论(0)    收藏  举报