首页  ·  知识 ·  云计算
CheckBoxList的用法
佚名  http://bbs.newasp.net/dispbbs.asp?boardid=21&Id=59  综合  编辑:dezai  图片来源:网络
1.绑定数据this.lngCatalogID.DataSource = dt; //这里我绑到DataTable上了.this.lngCatalogID.DataTextField = "strCatalogName"; //
1.绑定数据

this.lngCatalogID.DataSource = dt;  //这里我绑到DataTable上了.
this.lngCatalogID.DataTextField = "strCatalogName";  //前台看到的值,也就是CheckBoxList中显示出来的值
this.lngCatalogID.DataValueField = "lngCatalogID";  //这个值直接在页面上是看不到的,但在源代码中可以看到
this.lngCatalogID.DataBind();

2.获取钩选的项

    

for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
    if (CheckBoxList1.Items[i].Selected)
        Response.Write("你选的是" +CheckBoxList1.Items[i].Value+ CheckBoxList1.Items[i].Text + "
");
}
利用循环来顺序遍历每个选项,被选中的输出.

for (int i = 0; i < hfAnswers.Value.Split(',').Length; i++)//给CheckBoxList选中的复选框 赋值
{
    for (int j = 0; j < CBoxListAnswer.Items.Count; j++)
    {
        if (hfAnswers.Value.Split(',')[i] == CBoxListAnswer.Items[j].Value)
        {
            CBoxListAnswer.Items[j].Selected = true;
        }
    }
}

string m_strTemp = string.Empty;
for (int i = 0; i < CBoxListAnswer.Items.Count; i++)//读取CheckBoxList 选中的值,保存起来
{
    if (CBoxListAnswer.Items[i].Selected)
    {
        m_strTemp += CBoxListAnswer.Items[i].Value + ",";
    }
}
if (!string.IsNullOrEmpty(m_strTemp))
    Label1.Text = m_strTemp.Substring(0, m_strTemp.Length - 1);
else
    Label1.Text = m_strTemp;



3.设置某项为钩选状态

foreach(ListItem li in lngCatalogID.Items)
{
    if(li.Value.Equals("钩选条件"))    //如果li.Value值等于某值,就钩选
    {
        li.Selected = true;                    //等于true就表示钩选啦.
        break;
    }
}


数据绑定
checkedListBox1.DataSource=ds.Tables[0];
checkedListBox1.ValueMember="intSectionID";
checkedListBox1.DisplayMember="txtShortDesc".ToString();
数据显示
int count = checkedListBox1.Items.Count;
for (int i = 0;i{
    if (checkedListBox1.GetItemChecked(i))
    {
        MessageBox.Show(checkedListBox1.Items[i].ToString());
    }
}

DataGrid中全选
foreach(DataGridItem thisItem in DataGridLogininfo.Items)
{
    ((CheckBox)thisItem.Cells[0].Controls[1]).Checked = CheckBox2.Checked;
}

反向选择

for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
    if (checkedListBox1.GetItemChecked(i))
    {
        checkedListBox1.SetItemChecked(i, false);
    }
    else
    {
        checkedListBox1.SetItemChecked(i, true);
    }
} 本文作者:佚名 来源:http://bbs.newasp.net/dispbbs.asp?boardid=21&Id=59
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读