xingd.net

.net related techonology

导航

ASP.NET 动态创建控件引起的ViewState读取问题

Posted on 2005-03-02 15:39  xingd  阅读(8441)  评论(16编辑  收藏  举报
今天在开发ASP.NET上的控件时遇到了一个郁闷的问题,页面上有一个View Switch的功能要实现,可以选择使用Chart或是Grid来查看数据,包含了Grid Only, Chart Only, Grid with Chart等几种布局。开始的时候发现由Grid at Bottom到Grid at Right切换的时候,Chart的View State始终是null,后来实现Grid only到Grid at Right时,出现了以下的错误。

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.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: 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.

Stack Trace:

[HttpException (0x80004005): 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.]
   System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +189
   System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +414
   System.Web.UI.Control.AddedControl(Control control, Int32 index) +291
   System.Web.UI.ControlCollection.Add(Control child) +153
   USoft.Aurora.WebControls.AnalysisReport.CreateChildControls() in e:\project\aurora\code.v6\aurora\analysiswebcontrols\analysisreport.cs:172
   System.Web.UI.Control.EnsureChildControls() +100
   System.Web.UI.Control.PreRenderRecursiveInternal() +38
   System.Web.UI.Control.PreRenderRecursiveInternal() +125
   System.Web.UI.Control.PreRenderRecursiveInternal() +125
   System.Web.UI.Page.ProcessRequestMain() +1499

出错信息倒也详细,一看就知道出错原因,可是没想到Web Controls上还有这样的要求。

问题已经解决,但暂时不贴上来了,希望能听听大家的意见。