在上一个请求期间,控制树与viewstate不匹配

时间:2012-02-17 15:20:15

标签: asp.net viewstate

我正在调试一个非常复杂的系统,我没有编码。在帖子后面我得到这个错误:

Exception: 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.

我不太熟悉viewstate如何加载和工作。代码构建一些网格视图并控制dynamicaly并将它们插入名为 phCallsDynamicGridHolder

的placeHolder中

创建异常的代码总是与此类似:

HtmlGenericControl summary = createNewCallsSummary(calls);
GridView gv = createGridView(calls, "Calls", width);
phCallsDynamicGridHolder.Controls.Add(summary);
phCallsDynamicGridHolder.Controls.Add(gv);
phCallsDynamicGridHolder.Controls.Add(createNewCallsTotalSummary());

是否有人熟悉导致此类异常的原因?该页面几乎有2000行代码,我将根据要求提供尽可能多的代码。

2 个答案:

答案 0 :(得分:0)

心理调试,我猜你正在每个请求上进行动态控制创建;您可能想尝试更改代码,这样您只能在以下内容中创建动态控件:

if (!IsPostBack)
{
    // here
}

答案 1 :(得分:0)

尝试为此页面禁用ViewState。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind=" #####.aspx.cs" Inherits="#####" EnableViewState="false"%>