2012-05-04 129 views
1

我有在它 用新的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. 

IM的話非常事先沮喪,我會採取任何答案非常感謝 感謝

回答

1

你可能不需要跟蹤的ViewState該控件,因爲它是動態的,所以嘗試設置EnableViewState爲假並看看是否有幫助。

+0

感謝詹姆斯約翰遜我WORSHIP你!! @! –

相關問題