无法加载viewstate。加载viewstate的控件树必须与用于保存viewstate期间的cntrl树匹配。

时间:2015-05-04 09:14:06

标签: c# asp.net

我收到此错误

  

“无法加载viewstate。视图状态所在的控制树   正在加载必须与用于保存的控制树匹配   在上一个请求期间查看状态。例如,添加时   动态控制,回发期间添加的控件必须匹配   初始期间添加的控件的类型和位置   请求。 “

当我尝试提交一个页面,我在GridView rowdatabound上应用了一些逻辑来改变RowSpan。在评论此事件时没有错误。

这是代码:

    int firstRow;
    string previousCat;
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var drv = e.Row.DataItem as QCParameters;

            if (previousCat == drv.AuditTypeValue)
            {
                //If it's the same category as the previous one
                //Increment the rowspan
                if (GridView1.Rows[firstRow].Cells[0].RowSpan == 0)
                {
                    GridView1.Rows[firstRow].Cells[0].RowSpan = 2;
                    GridView1.Rows[firstRow].Cells[5].RowSpan = 2;
                }
                else
                {
                    GridView1.Rows[firstRow].Cells[0].RowSpan += 1;
                    GridView1.Rows[firstRow].Cells[5].RowSpan += 1;
                }
                //Remove the cell
                if (e.Row.Cells.Count > 5)
                    e.Row.Cells.RemoveAt(5);
                e.Row.Cells.RemoveAt(0);
            }
            else //It's a new category
            {                
                e.Row.VerticalAlign = VerticalAlign.Top;
                //Maintain the category in memory
                previousCat = drv.AuditTypeValue;
                firstRow = e.Row.RowIndex;
            }
        }
   }

1 个答案:

答案 0 :(得分:1)

问题是您删除了单元格,而不是在回发期间(重新创建控件树时)它不匹配,并且无法加载ViewState。 一种可能的解决方案是隐藏"通过设置e.Row.Cells[5].Visible = false;来设置细胞 不可见的控件不会被渲染,但仍然是页面控制树的一部分。