悟生慧

 

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;
                    //}

                }
        }

    }

posted on 2011-05-16 16:08  悟生慧  阅读(8054)  评论(1编辑  收藏  举报

导航