我在DataList模板里添加了一个CheckBox,我想让CheckBox被选中后,相应表格的数据写进另一个数据表里,请问这要怎样去做呢?

解决方案 »

  1.   

    我想让CheckBox被选中后,相应表格的数据写进另一个数据表里
    ==
    选中就写数据库?这个想法不好,因为用户频繁的点击CheckBox就会导致你的数据库的频繁访问建议外部用按钮确认,一次提交多个复选框选中项的数据
      

  2.   

    foreach (DataListItem item in DataList1.Items)
    {
        CheckBox chk = item.FindControl("CheckBox1") as CheckBox;
        if (chk != null && chk.Checked)
        {
            string id = DataList1.DataKeys[item.ItemIndex].ToString(); //用DataKeys得到主键集合,前提是设置过DataList的DataKeyField属性为表的主键
            //下面根据id进行你的数据库操作
        }
    }
      

  3.   

    string id = DataList1.DataKeys[item.ItemIndex].ToString();
    这里不是很明白,不用id,我也能操作了吧,我想将对应表格里里从一个数据表来的Label里的数据写进另一个数据表。
      

  4.   

    一般更新和删除都是基于主键的,这个主键来源于DataList的DataKeyField属性
      

  5.   

    如果你需要找DataList里的Label里的值
    foreach (DataListItem item in DataList1.Items)
    {
        CheckBox chk = item.FindControl("CheckBox1") as CheckBox;
        if (chk != null && chk.Checked)
        {
            string str = (item.FindControl("Label1") as Label).Text;
        //    string id = DataList1.DataKeys[item.ItemIndex].ToString(); //用DataKeys得到主键集合,前提是设置过DataList的DataKeyField属性为表的主键
            //下面根据id进行你的数据库操作
        }
    }
      

  6.   

    foreach (DataListItem item in DataList1.Items)
    {
        CheckBox chk = item.FindControl("CheckBox1") as CheckBox;
        if (chk != null && chk.Checked)
        {
            string str = ((Label)item.FindControl("Label1")).Text;   //你要的标签的值
             string sql="";你要执行的语句   
             执行语句
         }