CheckBoxList1复选框

时间:2024-01-07 11:29:02

循环绑定数据的两个方法:

List<string> LIColl = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
foreach (SPUser user in web.AllUsers)//遍历所有用户
{
CheckBoxList1.Items.Add(user.Name);//直接绑定
LIColl.Add(user.Name);

//approversCollection.Add(new SPFieldUserValue(web,user.ID,user.LoginName));

}

//CheckBoxList1.DataSource = LIColl;//后续绑定
//CheckBoxList1.DataBind();//后续绑定

读取

private static string selval;//读取后放到里面去
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{

for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
selval += CheckBoxList1.Items[i].Text + ";" + selval;
}
}

}

判断是否选中:

后台:

  ​int count = ;
                foreach (ListItem item in this.CheckBoxList1.Items)
                {
                    if (item.Selected)
                    {
                        count++;
                    }
                }
if (count == )
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), Guid.NewGuid().ToString(), string.Format("<script>alert('请选择人员!')</script>")); return;
}
else
{
}

前台js

<script type="text/javascript">

    function Copy() {
var dutyflag = 0;
//ctl00_ctl33_g_5faa55c8_cfe2_4734_9517_ee7dfc9e832d_ctl00_CheckBoxList1_0
var checkobj = document.getElementById("ctl00_ctl33_g_5faa55c8_cfe2_4734_9517_ee7dfc9e832d_ctl00_CheckBoxList1");
var checks = checkobj.getElementsByTagName("input");
for (var n = 0; n < checks.length; n++) {
if (checks[n].type == "checkbox" && checks[n].checked == true) {
dutyflag = 1;
}
}
if (dutyflag == 0) {
alert("至少要选择1个部门");
return false;
}
else {
var value = document.getElementById("<%=divnr.ClientID %>").innerHTML;
var real = value.replace(/<[^>]*>/g, "").replace(/&nbsp;/g, "").replace(/\s+/g, "");
if (real == "") {
//alert("real is null");
document.getElementById("<%=HiddenFieldnr.ClientID %>").value = "";
}
else {
//alert("real is not null: "+real)
document.getElementById("<%=HiddenFieldnr.ClientID %>").value = value;
}
var valu = document.getElementById("<%=divnb.ClientID %>").innerHTML;
var rea = valu.replace(/<[^>]*>/g, "").replace(/&nbsp;/g, "").replace(/\s+/g, "");
if (rea == "") {
//alert("real is null");
document.getElementById("<%=HiddenFieldnb.ClientID %>").value = "";
}
else {
//alert("real is not null: "+real)
document.getElementById("<%=HiddenFieldnb.ClientID %>").value = valu;
}
return true;
}
} </script>
<asp:Button ID="Button1" runat="server" Text="发送" OnClientClick="return Copy()" OnClick="Button1_Click" />