Table1 与 Table2
Datalist绑定的是【Table1】中的部分数据,现在我在Datalist里添加了Checkbox,页面中添加个button想实现的功能:点击button将选中的数据添加到表【Table2】中

解决方案 »

  1.   

    假设第一列有checkbox,id为ck1,下面是遍历      for (int i = 0; i < GridView1.Rows.Count; i++)
          {
              CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("ck1");
              if (cb.Checked)
              {
                  //这里写插入到tabel2代码
              }
          }
      

  2.   

          for (int i = 0; i < GridView1.Rows.Count; i++)
          {
              CheckBox cb = GridView1.Rows[i].FindControl("CheckBox1") as CheckBox;
              if (cb.Checked)
              {
                  插入table表;
              }
          }要在绑定完后执行。另外可以在RowDataBound事件里处理。          CheckBox cb = e.Row.FindControl("CheckBox1") as CheckBox;
              if (cb.Checked)
              {
                  插入table表;
              }
      

  3.   

    我用的是datalist绑定的数据,没有Rows的属性,代码该怎么写。
      

  4.   

          for (int i = 0; i < DataList1.Items.Count; i++)
          {
              CheckBox cb = DataList1.Items[i].FindControl("CheckBox1") as CheckBox;
              if (cb.Checked)
              {
                  插入table表;
              }
          }
      

  5.   

    冒昧再问下:
      插入表代码时除插入语句,还得实例化什么的操作吗?,还得写什么代码?谢谢!(insert into Table2(name2,message2,...)select name1,message1,... from Table1)
      

  6.   

    补充一下:
      要绑定到Datalist的表二(Table2)的字段(name,image,message)
      要添加到表一(Table1)字段(pname,pimage,pmessage)添加表一(Table1)方法:
    public int AddTable1(Table1 tb1)
        {
            string cmdstr = "insert into tb1(pname,pimage,pmessage)values(@psname,@psinfo,@psimage)";
            return Sqlhelp.ExecuteNonQuery(Sqlhelp.constr, cmdstr, new SqlParameter[]{
                new SqlParameter("@pname",tb1.Pname),
                new SqlParameter("@pimage",tb1.Pimage),
                new SqlParameter("@pmessage",tb1.Pmessage)
            });
        }