关于Asp.net 页面动态加载用户控件,出现“未能加载视图状态”的原因

我定义了两个用户控件。

Definition1 =(Definition) Page.LoadControl("ModuleControl/Definition.ascx");
                    
RightDefine1 
= (RightDefine) Page.LoadControl("RightControl/RightDefine.ascx");

根据页面的radio选择不同的用户控件加载,

private void Page_Load(object sender, System.EventArgs e)
        
{

            
//            Response.Write(ModuleType.ToString()+"select<br>");
//            Response.Write(ViewType_txt.Text+"select<br>");
//            
            if(ModuleType.ToString()!=ViewType_txt.Text)
            
{
                
                WebPart.EnableViewState 
= false;
                ViewType_txt.Text 
= ModuleType.ToString();
//                
//                _pageModule = Request.QueryString["PageModule"];
//                if(_pageModule!="")
//                    Response.Redirect(Request.Url+"?PageModule="+((int)ModuleDetail).ToString());
                
               
            }

            
            

            WebPart.Controls.Clear();
            
switch(ModuleType)
            
{
                
case (int)LoadContent.ModuleDetail:
                    
                    
                    Definition1 
=(Definition) Page.LoadControl("ModuleControl/Definition.ascx");
                    

                    
                    Definition1.PostShowCatalog 
+=new AuthorWeb.Web.ModuleControl.Definition.ShowCatalogEventHandler(Definition1_PostShowCatalog);
                    ModuleCatalog1.SelectedCatalog 
+=new AuthorWeb.Web.ModuleControl.ModuleCatalog.SelectCatalogEventHandler(ModuleCatalog1_SelectedCatalog);

                    Definition1.ID 
= "ID_Define";
                    WebPart.Controls.Add(Definition1);
                    
                

                    

//                    Response.Write("add define control<br>");
                    


                    
break;
                
case (int)LoadContent.ModuleRightDetail:


                                    
                    
                    RightDefine1 
= (RightDefine) Page.LoadControl("RightControl/RightDefine.ascx");
                    RightDefine1.ID 
= "ID_RightDefine";

                    WebPart.Controls.Add(RightDefine1);
                    ModuleCatalog1.SelectedCatalog 
+=new AuthorWeb.Web.ModuleControl.ModuleCatalog.SelectCatalogEventHandler(ModuleCatalog1_SelectedCatalog_byRight);
                    
                
                



                


                    
break;

            }

}

用户在几次使用中都会遇见:

未能加载视图状态。正在向其中加载视图状态的控件树必须与前一请求期间用于保存视图状态的控件树相匹配。例如,当以动态方式添加控件时,在回发期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配。


关于这个问题的讨论有:
http://www.cnblogs.com/airforce1st/archive/2004/08/27/36973.aspx
http://blog.joycode.com/ghj/archive/2004/04/16/19768.aspx

这个问题的出现,我认为就是ViewState造成的。

在回发时候,用户试图去加载第二个控件的时候,由于第一控件仍有ViewState存在,服务端会尝试去恢复已经不存在的用户控件的状态(或事件),结果,出现这种问题。

我作了一些尝试:

比如在加载不同的用户控件的时候,将PlaceHolder的EnableViewState = false;
给用户控件加上定义的id:

     Definition1.ID = "ID_Define";
     WebPart.Controls.Add(Definition1);
但是,运行的时候还是有问题。

最后实在没有办法,在同事ben的帮助下,加了一行代码,解决了问题。
我们一致认为这个是微软的bug
这行代码是什么呢??

WebPart.Controls.Add(new Label());//it is
     WebPart.Controls.Add(RightDefine1);


本文续:
http://www.cnblogs.com/king_astar/archive/2005/06/01/165763.html


 

posted on 2004-10-14 16:45  一望无际的南  阅读(9194)  评论(11编辑  收藏  举报

导航