日期:2014-05-18  浏览次数:20445 次

为什么页面重新加载后,dataset或datatable会为空?
像这样定义datatable,然后,在按钮的点击事件bFindSave_Click中,datatable内容却为空了,这是怎么回事?
 public partial class AuthorizePurchaseOrder : Page
  {
  private readonly DatabaseHelper db = new DatabaseHelper();
  private DataTable dt = new DataTable();

protected void FindPurchase(string Condition)
  {
  var sb = new StringBuilder();
  sb.Append("select code,b.realname Petitioner,c.realname,applydate,recorddate");
  sb.Append(
  ",(case state when 0 then '未提交' when 1 then '已提交' when 2 then '已审核' when 3 then '已完成' when 10 then '已作废' end ) state");
  sb.Append(",state stateid");
  sb.Append(" from tb_Purchase a,tb_user b,tb_user c");
  sb.Append(" where a.petitioner=b.userid and a.userid=c.userid");
  sb.Append(Condition);
  dt1 = db.ExecuteDataSet(sb.ToString()).Tables[0];
  uwgPurchase.DataKeyField = "code";
  dt1.PrimaryKey = new[] {dt1.Columns["code"]};

  uwgPurchase.DataBind();
  ViewState["dt1"] = dt1;
  }

  protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {

  FindPurchase("");  
  }
  }

 protected void bFindSave_Click(object sender, EventArgs e)
  {
  var sb = new StringBuilder();
  foreach (DataRow c in dt1.Rows) //此时,dt1为空了。
  {
  switch (c.RowState)
  {
  case (DataRowState.Added):
  break;
  case (DataRowState.Modified):
  // sb.Append(" update tb_Purchase set state=1 where code='"+c.Cells.FromKey("code")+"' and state=0");
  break;
  case (DataRowState.Deleted):
  sb.Append(" delete from tb_Purchase where code='" + c["code",DataRowVersion.Original] + "' and state=0");
  sb.Append(" delete from tb_PurchaseList where code='" + c["code", DataRowVersion.Original] + "'");
  break;
  }
  }
  var id = Convert.ToInt32(hfID.Value);

  foreach (DataRow c in dt.Rows)
  {
  switch (c.RowState)
  {
  case (DataRowState.Added):
  id++;
  sb.Append(" insert into tb_PurchaseList(code,id,commoditycode,quantity,comment)");
  sb.Append(" values('" + hfCode.Value + "'," + id + ",'" + c["code"] + "'," +
  c["quantity"]);
  sb.Append(",'" + c["comment"] + "')");
  break;
  case (DataRowState.Modified):
  sb.Append(" update tb_PurchaseList");
  sb.Append(" set CommodityCode='" + c["code"] + "'");
  sb.Append(&q