明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1277, 文章 - 0, 评论 - 214, 阅读 - 321万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Asp.Net :CheckBoxList用法

Posted on   且行且思  阅读(13248)  评论(0编辑  收藏  举报
string SupporFacilities_Value = "";
            //for (int i = 0; i < SupporFacilities_CheckBoxList.Items.Count; i++)
            //{
            //   if (SupporFacilities_CheckBoxList.Items[i].Selected)
            //       SupporFacilities_Value += SupporFacilities_CheckBoxList.Items[i].Text + ",";
            //}
            foreach (ListItem item in SupporFacilities_CheckBoxList.Items)
            {
                if (item.Selected == true)
                    SupporFacilities_Value += item.Text + ",";
            }
                                                                                                  

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>

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示