foreach (Control ct in this.Controls)
            {
                if (ct.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox"))
                {
                    CheckBox cb = (CheckBox)ct;
                    if (cb.Checked)
                    {
                        Stu.Gexing += cb.Text;
                    }
                }
            }
想遍历页面上所有的checkbox 然后把选中的值添到数据库 这个代码是网上找的 就是进不了if里面

解决方案 »

  1.   

    跟踪一下,看看ct.GetType().ToString()是什么就行了
      

  2.   

            foreach (Control ct in this.Controls)
            {
                if (ct is CheckBox)
                {
                    CheckBox cb = (CheckBox)ct;
                    if (cb.Checked)
                    {
                        Stu.Gexing += cb.Text;
                    }
                }
            }
      

  3.   

    foreach (Control ct in this.form1.Controls) {
                    if (ct is CheckBox) {
                        CheckBox cb = (CheckBox)ct;
                        if (cb.Checked) {
                            Stu.Gexing += cb.Text;
                        }
                    }
                }
      

  4.   

            private void SetStus(Control c)
            {
                if (c.HasChildren)
                {
                    foreach (Control ct in c.Controls)
                    {
                        SetStus(ct);
                    }
                }
                
                if (c is CheckBox)
                {
                    CheckBox cb = (CheckBox)c;
                    if (c.Checked)
                    {
                        Stu.Gexing += c.Text;
                    }
                }
            }调用:
    SetStus(this);