1.绑定数据
this.lngCatalogID.DataSource = dt; //\绑到DataTable上
this.lngCatalogID.DataTextField = "strCatalogName"; //前台看到的值,也就是CheckBoxList中显示出来的值
this.lngCatalogID.DataValueField = "lngCatalogID"; //这个值直接在页面上是看不到的,但在源代码中可以看到
this.lngCatalogID.DataBind();
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
}
}
{
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;
break;
}
}
{
if(li.Value.Equals("钩选条件")) //如果li.Value值等于某值,就钩选
{
li.Selected = true;
break;
}
}
反向选择
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
checkedListBox1.SetItemChecked(i, false);
}
else
{
checkedListBox1.SetItemChecked(i, true);
}
}
{
if (checkedListBox1.GetItemChecked(i))
{
checkedListBox1.SetItemChecked(i, false);
}
else
{
checkedListBox1.SetItemChecked(i, true);
}
}
#region 设置或者得到CheckBoxList选中了的值
/// <summary>
/// 初始化CheckBoxList中哪些是选中了的
/// </summary>
/// <param name="checkList">CheckBoxList</param>
/// <param name="selval">选中了的值串例如:"0,1,1,2,1"</param>
/// <param name="separator">值串中使用的分割符例如"0,1,1,2,1"中的逗号</param>
public string SetChecked(CheckBoxList checkList,string selval,string separator)
{
selval = separator + selval + separator; //例如:"0,1,1,2,1"->",0,1,1,2,1,"
for(int i=0; i<checkList.Items.Count; i++)
{
checkList.Items[i].Selected = false;
string val = separator + checkList.Items[i].Value + separator;
if(selval.IndexOf(val)!=-1)
{
checkList.Items[i].Selected = true;
selval = selval.Replace(val,separator); //然后从原来的值串中删除已经选中了的
if(selval == separator) //selval的最后一项也被选中的话,此时经过Replace后,只会剩下一个分隔符
{
selval += separator; //添加一个分隔符
}
}
}
selval = selval.Substring(1,selval.Length-2); //除去前后加的分割符号
return selval;
}
///// <summary>
/// 得到CheckBoxList中选中了的值
/// </summary>
/// <param name="checkList">CheckBoxList</param>
/// <param name="separator">分割符号</param>
/// <returns>01,02,03</returns>
public string GetChecked(CheckBoxList checkList, string separator)
{
string selval = "";
for (int i = 0; i < checkList.Items.Count; i++)
{
if (checkList.Items[i].Selected)
{
selval += checkList.Items[i].Value + separator;
}
}
if (selval.Length > 1)
{
selval = selval.Substring(0, selval.Length - 1);
}
return selval;
}
/// <summary>
/// 初始化CheckBoxList中哪些是选中了的
/// </summary>
/// <param name="checkList">CheckBoxList</param>
/// <param name="selval">选中了的值串例如:"0,1,1,2,1"</param>
/// <param name="separator">值串中使用的分割符例如"0,1,1,2,1"中的逗号</param>
public string SetChecked(CheckBoxList checkList,string selval,string separator)
{
selval = separator + selval + separator; //例如:"0,1,1,2,1"->",0,1,1,2,1,"
for(int i=0; i<checkList.Items.Count; i++)
{
checkList.Items[i].Selected = false;
string val = separator + checkList.Items[i].Value + separator;
if(selval.IndexOf(val)!=-1)
{
checkList.Items[i].Selected = true;
selval = selval.Replace(val,separator); //然后从原来的值串中删除已经选中了的
if(selval == separator) //selval的最后一项也被选中的话,此时经过Replace后,只会剩下一个分隔符
{
selval += separator; //添加一个分隔符
}
}
}
selval = selval.Substring(1,selval.Length-2); //除去前后加的分割符号
return selval;
}
///// <summary>
/// 得到CheckBoxList中选中了的值
/// </summary>
/// <param name="checkList">CheckBoxList</param>
/// <param name="separator">分割符号</param>
/// <returns>01,02,03</returns>
public string GetChecked(CheckBoxList checkList, string separator)
{
string selval = "";
for (int i = 0; i < checkList.Items.Count; i++)
{
if (checkList.Items[i].Selected)
{
selval += checkList.Items[i].Value + separator;
}
}
if (selval.Length > 1)
{
selval = selval.Substring(0, selval.Length - 1);
}
return selval;
}
#endregion
来源:https://www.cnblogs.com/luomingui/archive/2011/08/04/2127495.html