zoukankan      html  css  js  c++  java
  • 关于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


  • 相关阅读:
    word,excel,ppt转Pdf,Pdf转Swf,通过flexpaper+swftools实现在线预览
    Node做中转服务器,转发接口
    Vue——路由回退至指定页面
    Vue——前端生成二维码
    解决移动端键盘弹起导致的页面fixed定位元素布局错乱
    Vue——手机号、验证码登录(设置按钮60s禁用倒计时)
    Vue——解决报错 Computed property "****" was assigned to but it has no setter.
    typescript 起步之安装及配置 ts-node 环境变量
    区分 for...in 和 for...of
    解决HTML5(富文本内容)连续数字、字母不自动换行
  • 原文地址:https://www.cnblogs.com/king_astar/p/52322.html
Copyright © 2011-2022 走看看