ASP.NET 后台动态生成CheckBOx控件并获取选中的值

    CheckBox checkBox;

    public void CheckBoxNew()
    {
        ZhuCeTable = BllZhuCeLogic.GetDataZhuCe("proc_GetTheGongChengInfo", "101050", "");
        for (int i = 0; i < ZhuCeTable.Rows.Count; i++)
        {
            checkBox = new CheckBox();
            checkBox.AutoPostBack = true;
            checkBox.Text = ZhuCeTable.Rows[i]["SuoShuQu"].ToString();
            checkBox.ID = ZhuCeTable.Rows[i]["BeianHao"].ToString();
            checkBox.CheckedChanged += eventFunction;//eventFuntion是事件处理的函数
            this.Panel1.Controls.Add(checkBox);
        }
    }
    protected void eventFunction(object sender, EventArgs e)
    {
        for (int i = 0; i < this.Panel1.Controls.Count - 1; i++)
        {
            CheckBox ckf = (CheckBox)Panel1.FindControl(ZhuCeTable.Rows[i]["BeianHao"].ToString());
            if (ckf != null)
            {
                if (ckf.Checked == true)
                {
                    CheckBox ckf1 = (CheckBox)Panel1.FindControl(ZhuCeTable.Rows[i]["BeianHao"].ToString());
                    ckf1.Checked = true;
                    Response.Write("<script>alert('" + ckf1.Text + "')</script>");
                }
                else
                {
                    if (ckf.Checked == false)
                    {
                        CheckBox ckf1 = (CheckBox)Panel1.FindControl(ZhuCeTable.Rows[i]["BeianHao"].ToString());
                        ckf1.Checked = false;
                    }
                }
            }
        }
    }
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页