遍历checkbox

来源:互联网 发布:centos下使用git 编辑:程序博客网 时间:2024/06/06 03:30

            string mySelect = "";


     //web控件
            foreach (Control ct in form1.Controls)
            {
                if (ct.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox"))
                {
                    CheckBox cb = (CheckBox)ct;
                    if (cb.Checked == true)
                    {
                        if (mySelect == "")
                        {
                            mySelect += cb.Value;
                        }
                        else
                        {
                            mySelect += "~" + cb.Value;
                        }
                    }
                }
            }

 


     //html控件
            foreach (Control ct in form1.Controls)
            {
                if (ct.GetType().ToString().Equals("System.Web.UI.HtmlControls.HtmlInputCheckBox"))
                {
                    HtmlInputCheckBox cb = (HtmlInputCheckBox)ct;
                    if (cb.Checked == true)
                    {
                        if (mySelect == "")
                        {
                            mySelect += cb.Value;
                        }
                        else
                        {
                            mySelect += "~" + cb.Value;
                        }
                    }

                }
            }
            Page.RegisterStartupScript("alert", "<script language='javascript'> alert('" + mySelect + "!');</script>");