datagrid专栏2

来源:互联网 发布:java classpath linux 编辑:程序博客网 时间:2024/06/13 01:07

一。this.btnDelete.Attributes.Add("onclick","return confirm('确定要删除么?')");
   BLL.Product product = new admin.BLL.Product(); 
   foreach(DataGridItem dgItem in this.DataGrid1.Items)
   {
    CheckBox chk = (CheckBox)dgItem.FindControl("chkDelete");
    if(chk.Checked ==true)
    {
       
            product.DeleteProduct(dgItem.Cells[0].Text);//dgItem.Cells[0].Text获取指定单元格的数据,必须是绑定列
    }

   }
            this.BindGrid(); 

二。for (int i=0; i < this.dgCart.Items.Count; i++)
   {

    // 找到某行的数量信息和删除信息。
    TextBox quantityTxt = (TextBox) dgCart.Items[i].FindControl("Quantity");
    CheckBox remove = (CheckBox) dgCart.Items[i].FindControl("Remove");
               
    // 出错处理。防止用户的非法输入,如quanlity为负数等
    int quantity;
    try
    {
     quantity = Int32.Parse(quantityTxt.Text);

     // 如果数量被修改或者Remove复选框被选中
     if((quantity != Convert.ToInt32(dgCart.DataKeys[i])) || (remove.Checked == true))
     {

      Label lblBookID = (Label) dgCart.Items[i].FindControl("bookID");
      //数量为0或用户选择删除
      if (quantity == 0 || remove.Checked == true)
      {
       cart.DeleteItem(cartID, Int32.Parse(lblBookID.Text));
      }
      else
      {
       cart.UpdateItem(cartID, Int32.Parse(lblBookID.Text),quantity);
      }
     }
    }
    catch
    {
     lblError.Text = "对不起,您的输入信息有误!";
    }
   }

三。获取摸板列中的控件

(类型)e.Item.FindControl("TextBox1");//在知道控件名的情况下

(类型)e.Item.Cell[2].Control[0]