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


    DataTable dtES = ExperienceShopInfo.getExperienceShopAll();
        CheckBox checkBox;
        public void CheckBoxNew()
        {
            for (int i = 0; i < dtES.Rows.Count; i++)
            {
                //Table table = new Table();
                //TableRow row = new TableRow();
                //TableCell cell = new TableCell();

                checkBox = new CheckBox();
                checkBox.AutoPostBack = true;
                checkBox.Text = dtES.Rows[i]["es_ShopName"].ToString();
                checkBox.ID = dtES.Rows[i]["es_No"].ToString();
                //checkBox.ID = "ck" + i;

                checkBox.CheckedChanged += eventFunction;//eventFuntion是事件处理的函数
                //cell.Controls.Add(checkBox);
                //row.Cells.Add(cell);
                //table.Rows.Add(row);
                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("ck" + i);
                CheckBox ckf = (CheckBox)Panel1.FindControl(dtES.Rows[i]["es_No"].ToString());

                if (ckf.Checked == true)
                {
                    // ckf.Checked = true;

                    //for (int j = 0; j < count[i]; j++)
                    //{

                    //CheckBox ckf1 = (CheckBox)Panel1.FindControl("ck" + i);
                    CheckBox ckf1 = (CheckBox)Panel1.FindControl(dtES.Rows[i]["es_No"].ToString());
                    ckf1.Checked = true;

                    Response.Write("<script>alert('" + ckf1.Text + "')</script>");
                    //}

                }
                else
                    if (ckf.Checked == false)
                    // CheckBox ckf = (CheckBox)Panel1.FindControl("ck"+i);
                    {
                        // ckf.Checked = true;
                        //for (int j = 0; j < count[i]; j++)
                        //{
                        //CheckBox ckf1 = (CheckBox)Panel1.FindControl("ck" + i);
                        CheckBox ckf1 = (CheckBox)Panel1.FindControl(dtES.Rows[i]["es_No"].ToString());
                        ckf1.Checked = false;
                        //}

                    }
            }

        }

  • 相关阅读:
    BZOJ 3053 The Closest M Points
    Python 语言介绍
    计算机组成与操作系统基础
    Gym 100818I Olympic Parade(位运算)
    Codeforces 602B Approximating a Constant Range(想法题)
    Codeforces 599D Spongebob and Squares(数学)
    Codeforces 599C Day at the Beach(想法题,排序)
    ZOJ 3903 Ant(数学,推公示+乘法逆元)
    ZOJ 3911 Prime Query(线段树)
    UVALive 6910 Cutting Tree(离线逆序并查集)
  • 原文地址:https://www.cnblogs.com/wuhuisheng/p/2047791.html
Copyright © 2020-2023  润新知