CheckBoxList1复选框

 

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

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 = 0;
                foreach (ListItem item in this.CheckBoxList1.Items)
                {
                    if (item.Selected)
                    {
                        count++;
                    }
                }
                if (count == 0)
                {
                    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" />

 

转载于:https://www.cnblogs.com/914556495wxkj/p/3630527.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值