博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
checkboxlist详细用法、checkboxlist用法、checkboxlist
阅读量:7054 次
发布时间:2019-06-28

本文共 2786 字,大约阅读时间需要 9 分钟。

for (int i = 0; i < CheckBoxList1.Items.Count; i++)

{
      if (CheckBoxList1.Items[i].Selected)
      Response.Write("你选的是" +CheckBoxList1.Items[i].Value+ CheckBoxList1.Items[i].Text + "<br>");
}
利用循环来顺序遍历每个选项,被选中的输出.
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;
1.绑定数据
    this.lngCatalogID.DataSource = dt; //这里我绑到DataTable上了.
    this.lngCatalogID.DataTextField = "strCatalogName"; //前台看到的值,也就是CheckBoxList中显示出来的值
    this.lngCatalogID.DataValueField = "lngCatalogID"; //这个值直接在页面上是看不到的,但在源代码中可以看到
    this.lngCatalogID.DataBind();
2.获取钩选的项
foreach(ListItem li in lngCatalogID.Items)
    {
     if(li.Selected)    //表示某一项被选中了
     {   
            //li.Test表示看到的值,对应上面的strCatalogName
            //li.Value表示看到的值对应的值.对应上面的lngCatalogID
      }    
    }
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<count;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);
                }
            }
checkboxlist控件用法范例
范例一:
<script language="c#" runat="server">
public void Item_changed(Object sender,EventArgs e)
{
string str;
foreach(ListItem item in cblist.Items)
{
    if(item.Selected)
      mylabel.Text+="<hr><li>"+item.Text;
}
}
</script>
<html>
<head><title>checkbox test page</title></head>
<body>
<form runat="server">
<asp:checkboxlist id="cblist" runat="server">
<asp:listitem text="checkbox1"/>
<asp:listitem text="checkbox2"/>
<asp:listitem text="checkbox3"/>
<asp:listitem text="checkbox4"/>
</asp:checkboxlist>
<asp:button id="btn1" text="click me" OnClick="Item_changed" runat="server"/>
<hr>
<asp:label id="mylabel" runat="server"/>
</form>
</body>
</html>

转载于:https://www.cnblogs.com/xingvskong11/archive/2012/08/01/checkboxlist.html

你可能感兴趣的文章
5G一周热闻:华为夺联通5G大单,首张5G电话卡发放
查看>>
调研对敏捷宣言2.0的需求
查看>>
微软在C# 8中引入预览版可空引用类型
查看>>
深究JavaScript——函数调用与this详解
查看>>
书评与访谈:Software Development Metrics
查看>>
re:Invent第二天:互联网客户在右传统客户在左,AWS向哪儿?
查看>>
云端能力知几许?12人众测华为云企业级Kubernetes集群实力
查看>>
《Elixir in Action》书评及作者问答录
查看>>
Apache HBase的现状和发展
查看>>
AlphaZero进化论:从零开始,制霸所有棋类游戏
查看>>
IBM中国开发中心吉燕勇: 通过Cloud Data Services打造新型认知计算数据分析云平台...
查看>>
作者问答:解密硅谷
查看>>
linux系统高并发socket最大连接数优化
查看>>
Netflix发布Polly.JS,一个用于HTTP交互的开源库
查看>>
敏捷团队中测试人员的角色
查看>>
GitHub推出Scientist,帮助开发者重构关键路径代码
查看>>
40%创业公司用伪AI忽悠钱,欧洲被AI时代抛弃了吗?
查看>>
AT&T签署8位数合同,设备商恐无法从5G获利
查看>>
Netflix Play API:我们为什么构建了一个演进式架构?
查看>>
我不是仆人,是主人!敏捷中领导力的新比喻?
查看>>