更新多行函数

来源:互联网 发布:淘宝开店交钱 编辑:程序博客网 时间:2024/06/07 06:25

protected void updateButton_Click(object sender, EventArgs e)
{
  int rowsCount = grid.Rows.Count;

  GridViewRow gridRow;

  TextBox quantityTextBox;

  string productId;

  int quantity;

  bool success = true;
  // 遍历GridView中的每一行
  for (int i = 0; i < rowsCount; i++)
  {
    // 获行当前行
    gridRow = grid.Rows[i];
    // 通过DATAKEYS来取行没显示出来的ID号
    Id = grid.DataKeys[i].Value.ToString();
    //
    quantityTextBox = (TextBox)gridRow.FindControl("editQuantity");
    // 转换为整形,如果输入的是非法字符Int32.TryParse返回FALSE
    if (Int32.TryParse(quantityTextBox.Text, out quantity))
    {
      // 调用业务层的方法更新数据
      success = success && BLL.UpdateItem(Id, quantity);
    }
    else
    {
      // 更新失败
      success = false;
    }
    // 显示信息
    statusLabel.Text = success ?
      "<br />更新成功!<br />" :
      "<br />更新失败!<br />";
  }
  // 重新绑定GridVIEW
  PopulateGridView();
}

 
原创粉丝点击