【问题标题】:Failed to load viewstate.The control tree into which viewstate is being loaded must match the cntrl tree that was used to save viewstate during加载视图状态失败。正在加载视图状态的控制树必须与用于保存视图状态的 cntrl 树匹配
【发布时间】:2015-07-13 14:55:49
【问题描述】:

我收到这个错误

"加载视图状态失败。视图状态所在的控件树 正在加载的必须与用于保存的控制树匹配 上一个请求期间的视图状态。例如,当添加 动态控制,回发期间添加的控件必须匹配 在初始阶段添加的控件的类型和位置 要求。 "

当我尝试提交一个页面时,我在 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;
            }
        }
   }

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复.. e.Row.Cells[5].Visible = false;对我来说效果更好。
    • @BhaweshPaliwal 非常正确。我也是这么想的。
    猜你喜欢
    • 2012-05-14
    • 2011-01-31
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多