C# 未能加载viewstate。正在加载viewstate的控件树必须与用于保存viewstate的控件树匹配

C# 未能加载viewstate。正在加载viewstate的控件树必须与用于保存viewstate的控件树匹配,c#,asp.net,gridview,dynamic,C#,Asp.net,Gridview,Dynamic,我有一个gridview,它创建了一个包含新gridview的新行 创建第二个gridview的方法是: protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "PopExtendedGrid") { GridView _gridView = (GridView)sender; int _rowIndex2 = int.

我有一个gridview,它创建了一个包含新gridview的新行 创建第二个gridview的方法是:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "PopExtendedGrid")
    {


    GridView _gridView = (GridView)sender;
int _rowIndex2 = int.Parse(e.CommandArgument.ToString());
GridView _ChildGrid = new GridView();
Control x = _gridView.Rows[_rowIndex2 + 1].Cells[1].Controls[1];
int Oid = int.Parse(((Label)x).Text);
_ChildGrid.DataSource = hs.GetExtendedGrid(Oid);
_ChildGrid.ID = "ChildGrid";
_ChildGrid.AutoGenerateColumns = false;
_ChildGrid.CssClass = "ChildGridS";
_ChildGrid.HeaderStyle.CssClass = "CreateHead";
BoundField one = new BoundField();
one.DataField = "ItemID";
one.HeaderText = "קוד מוצר";
_ChildGrid.Columns.Add(one);

BoundField two = new BoundField();
two.DataField = "ItemName";
two.HeaderText = "שם מוצר";
_ChildGrid.Columns.Add(two);

BoundField three = new BoundField();
three.DataField = "ItemSize";
three.HeaderText = "גודל מוצר";
_ChildGrid.Columns.Add(three);

GridViewRow tr = new GridViewRow(_rowIndex2 + 2 +10*this.GridView1.PageIndex,-1 , DataControlRowType.EmptyDataRow , DataControlRowState.Normal);
TableCell tc = new TableCell();
tc.ColumnSpan = _gridView.Columns.Count;
tc.Controls.Add(_ChildGrid);
tr.Cells.Add(tc);
if ((DataView)Session["dataSource"] != null)
 {
 DataView dv = (DataView)Session["dataSource"];
 this.GridView1.DataSource = dv;
 this.GridView1.DataBind();
 }
                else
                {
                    if (Session["lvl"].ToString() == "high")
                    {
                        PopulateGridViewAdmin();
                    }
                    else
                    {
                        PopulateGridViewUser();
                    }
                }
this.GridView1.Controls[0].Controls.AddAt(_rowIndex2 + 2, tr);
Session["ChildIndex"] = (_rowIndex2 + 2).ToString();
_ChildGrid.DataBind();
  }
}
我想当我需要执行另一个命令或与gridview相关的东西时,我会像这样删除行:

this.GridView1.Controls[0].Controls.RemoveAt(int.Parse(Session["ChildIndex"].ToString()));
然后重新填充mastergridview,但在我有机会这样做之前,此错误会不断弹出:

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.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
我对此感到非常沮丧,我将非常感激地接受任何回答
提前感谢

您可能不需要跟踪该控件的视图状态,因为它是动态的,所以请尝试将
启用视图状态
设置为false,看看是否有帮助。

您可能不需要跟踪该控件的视图状态,因为它是动态的,所以请尝试将
启用视图状态
设置为false,看看是否有帮助