0

我有一个DataSet14 rows绑定到的GridView1
GridView1有一个Edit (image) command
当用户单击第 1 行时RowEditing event,我将删除中的特定行(第 0 行)DataSet1并更新GridView1
EditIndex到 0 并重新绑定DataSet1GridView1.

GridView1.EditIndex = 0;
GridView1.DataSource = DataSet1;
GridView1DataBind();

当用户取消编辑时会出现问题。我收到错误消息:

Failed to load viewstate.  
The control tree into which viewstate is being loaded must match the control 
tree that was used to save viewstate during the previous request.  

例如,当动态添加控件时,回发期间添加的控件必须与初始请求期间添加的控件的类型和位置相匹配。

这似乎是因为原始EditIndex(第 1 行)和EditIndex我设置的新(第 0 行)不匹配。

4

1 回答 1

0

我找到了解决方案。ViewState 中的不匹配是因为在更新 GridView1.EditIndex 时,事件参数字段 e.NewEditIndex 值(我猜是更新 ViewState)没有更新。请参阅下面有效的更新代码:

GridView1.EditIndex = 0;
e.NewEditIndex = 0;
GridView1.DataSource = DataSet1;
GridView1DataBind();
于 2013-05-27T06:21:11.813 回答