当前位置:编程学习 > C#/ASP.NET >>

panel中通过数据库数据动态添加的checkbox如何通过选中的checkbox删除数据

如图


panel也是通过数据库中的分组数据进行动态添加的。我想通过foreach循环查找动态添加的checkbox查不到

代码如下:
 //窗体加载方法
        public void Loding()
        {
            xpPanelGroup2.Controls.Clear();
            string sql1 = "";
            string sql = "";
            int num = 0;
            ArrayList list = new ArrayList();
            string selectu = TxtSelect.Text;
            if (selectu == "" || selectu == null)
            {
                sql1 = "select * from Groups";
            }
            else
            {
                sql1 = "select * from Groups where id like (select GroupId from Users where UserName like '" + TxtSelect.Text + "')";
            }

            SqlDataAdapter da = new SqlDataAdapter(sql1, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "GroupName");
            dt = ds.Tables["GroupName"];
            int z = 10, a = 40, tmp1 = 1, k = 10, l = 40;

            for (int j = 0; j < dt.Rows.Count; j++)
            {
                UIComponents.XPPanel xp1 = new UIComponents.XPPanel();
                xp1.Location = new Point(z + 10, a + 10);
                xp1.Name = dt.Rows[j]["ID"].ToString();
                xp1.Size = new System.Drawing.Size(70, 400);
                xp1.TabIndex = 1;
                xp1.Caption = dt.Rows[j]["GroupName"].ToString();
                xpPanelGroup2.Controls.Add(xp1);


                UIComponents.XPPanelGroup xg = new XPPanelGroup();
                xg.Location = new Point(k + 10, l + 10);
                xg.Size = new System.Drawing.Size(265, 320);
                xp1.Controls.Add(xg);


                if (selectu == "" || selectu == null)
                {
                    sql = "select * from Users where GroupID='" + xp1.Name + "'  ";
                }
                else
                {
                    sql = "select * from Users where UserName='" + TxtSelect.Text + "'  "; ;
                }
                SqlDataAdapter da1 = new SqlDataAdapter(sql, conn);
                DataSet ds1 = new DataSet();
                da1.Fill(ds1, "Users");
                dt1 = ds1.Tables["Users"];
                int x = 10, y = 0, tmp = 1;


                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    CheckBox ck = new CheckBox();
                    ck.Location = new Point(x + 10, y + 10);
                    ck.Name = dt1.Rows[i]["id"].ToString();
                    ck.Size = new System.Drawing.Size(20, 23);
                    ck.UseVisualStyleBackColor = true;
                    ck.Click += new EventHandler(ck_Click);  //button的单击事件
                    xg.Controls.Add(ck);

                    PictureBox pb = new PictureBox();
                    pb.Location = new Point(x + 20, y + 10);
                    pb.ImageLocation = dt1.Rows[i]["Images"].ToString();
                    if (pb.ImageLocation == "" || pb.ImageLocation == null)
                    {
                        pb.Image = imageList1.Images[1];
                    }
                    pb.Size = new System.Drawing.Size(30, 23);
                    pb.SizeMode = PictureBoxSizeMode.StretchImage;
                    xg.Controls.Add(pb);

                    Label lb = new Label();
                    lb.Location = new Point(x + 50, y + 18);
                    lb.Text = dt1.Rows[i]["UserName"].ToString();
                    lb.Size = new System.Drawing.Size(100, 23);
                    xg.Controls.Add(lb);

                    Button btn = new Button();
                    btn.Location = new System.Drawing.Point(x + 150, y + 13);
                    btn.Name = dt1.Rows[i]["UserName"].ToString();
                    btn.Size = new System.Drawing.Size(30, 23);
                    btn.Image = imageList1.Images[1];
                    btn.UseVisualStyleBackColor = true;
                    btn.Click += new EventHandler(btn_Click);  //button的单击事件
                    xg.Controls.Add(btn);



                    Button btn1 = new Button();
                    btn1.Location = new System.Drawing.Point(x + 180, y + 13);
                    btn1.Name = dt1.Rows[i]["UserName"].ToString();
                    btn1.Size = new System.Drawing.Size(30, 23);
                    btn1.FlatStyle = FlatStyle.Flat;
                    btn1.Image = imageList1.Images[2];
                    btn1.UseVisualStyleBackColor = true;
                    btn1.Click += new EventHandler(btn1_Click);  //button的单击事件
                    xg.Controls.Add(btn1);

                    num++;
                    if (CheckAll.Checked == true)
                    {
                        ck.Checked = true;
                    }
                    int n = i % 1;
                    if (n == 0 && i != 0)
                    {
                        tmp++;
                    }
                    y = 30 * tmp;     //这里定义的x和y便于控制控件在界面上的位置
                    //foreach (Control ctl in this.xpPanelGroup2.Controls)
                    //{
                    //    if (ctl is CheckBox)
                    //    {
                    //        MessageBox.Show(ctl.Name);
                    //       
                    //    }
                    //}

                }
                
                int n1 = j % 1;
                if (n1 == 0 && j != 0)
                {
                    tmp1++;
                }
                a = 40 * tmp1;
                
            }

        }

急 在线等。。。。。
补充:.NET技术 ,  .NET Framework
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,