1.datalist中放入1个checkbox,共7条数据,也就是显示7个checkbox,如何判断只选中其中的4个?
2.如何得到当前所选中的checkbox的值?

解决方案 »

  1.   

    11.checkbox

    //checkbox验证
    public bool IsCHBValid()
    {
    bool flag=false;
    string sort="";
    foreach(ListItem i in this.CheckBoxList1.Items)
    {
    if(i.Selected==true)
    {
    sort+=i.Value;
    }
    } if( sort!="" )
    {
    flag=true;
    }
    return flag;
    }
      

  2.   

    在Button的click事件下:
        protected void Button1_Click(object sender, EventArgs e)
        {
            int a=0;
            for (int i = 0; i < DataList1.Items.Count; i++)
            {
                if (((CheckBox)DataList1.Items[i].FindControl("CheckBox1")).Checked==true)
                    a += 1;
            }
            if(a==4)
                Response.Write("你选中了4项!!");
        }
      

  3.   

    在每一个checkbox的click事件添加如下代码bool status=CheckStatus();
    if(!status)
    {
    this.checkbox1.checked=false;
    }
    在类中添加如下代码private bool CheckStatus()
    {
    int a=0;
    for (int i = 0; i < DataList1.Items.Count; i++) 
            { 
                if (((CheckBox)DataList1.Items[i].FindControl("CheckBox1")).Checked==true) 
                    a += 1; 
            } 
            if(a==4) 
                return false;
    return ture; }
      

  4.   

     <asp:CheckBox ID="single_Chk"  name="selectAll" runat="server" />
     <asp:HiddenField  ID="commentid" Value='<%#Eval("commentid")%>' runat="server"/>  
    //删除选中的数据。可全删除。全选根据ID删除
        protected void delWriteLog_Click(object sender, ImageClickEventArgs e)
        {
            sqlt.SetSQLBeginTrans();
            try
            {
                for (int i = 0; i < rpMyReceiveComment.Items.Count; i++)
                {
                    CheckBox CB = (CheckBox)rpMyReceiveComment.Items[i].FindControl("single_Chk");
                    if (CB.Checked == true)
                    {
                        string delID = ((HiddenField)rpMyReceiveComment.Items[i].FindControl("commentid")).Value;
                        cookies = Request.Cookies["UserCookies"];
                        if (cookies != null)
                        {
                            int uid = int.Parse(cookies["User_Uid"].ToString());//
                            sqlt.Operate("update a set a.delstate=1 from dbo.space_MyComments a inner join dbo.space_MyReleased b on a.logid=b.logid  where a.commentid=" + delID + " and b.uid=" + uid);
                        }
                     }
                } sqlt.SetSQLCommitTrans(); sqlt.ConClose();
            }
            catch
            {
                sqlt.SetSQLRollBack(); sqlt.ConClose(); base.MessageBox("00006", null);
            }
            this.PageFen();
        }
      

  5.   

    选中任意四项,点击事件然后循环得到选中的值 for (int i = 0; i < rpMyReceiveComment.Items.Count; i++)
                {
                    CheckBox CB = (CheckBox)rpMyReceiveComment.Items[i].FindControl("single_Chk");
                    if (CB.Checked == true)
                    {
                        string delID = ((HiddenField)rpMyReceiveComment.Items[i].FindControl("commentid")).Value;//选中的四项值
                    }
                }
      

  6.   

    单个选择,再循环判断。
    foreach ( DataListItem item in DataList1.Items )
    {
    CheckBox chk = (CheckBox) item.FindControl("CheckBox1");
    if(chk.Checked)
    {
    }
    }
      

  7.   

    这是不可能滴!!
    一般的错误是id错!!你看是否打错了!!
     if (((CheckBox)DataList1.Items[i].FindControl("CheckBox1")).Checked==true) 
      

  8.   

     protected void CheckBoxList2_SelectedIndexChanged(object sender, EventArgs e)
            {            for (int i = 0; i < shu; i++)
                {
                    if (CheckBoxList2.Items[i].Selected)
                    {
                        s1 = s1 + CheckBoxList2.Items[i].Text + ",";                }            }
                 Label10.Text = s1 ;
                Label6.Text = Label10.Text + Label11.Text;
                string str1 = Label6.Text.ToString();
                string[] astr = str1.Split(',');
                intCnt = astr.Length - 1;
                if (intCnt > shu)
                {
                    Response.Write("<script>alert('最多只能选"+shu+"个')</script>");
                    Response.AddHeader("refresh", "0");
                }
            }        public void panduan()
            {
                for (int i = 0; i < shu; i++)
                {
                    if (CheckBoxList2.Items[i].Selected)
                    {
                        s1 = s1 + CheckBoxList2.Items[i].Text + ",";                }            }
                Label10.Text = s1;
                Label6.Text = Label10.Text + Label11.Text;
                string str1 = Label6.Text.ToString();
                string[] astr = str1.Split(',');
                intCnt = astr.Length - 1;
                if (intCnt > shu)
                {
                    Response.Write("<script>alert('最多只能选" + shu + "个')</script>");
                    Response.AddHeader("refresh", "0");
                }
            }