现在的位置: 首页 > 综合 > 正文

使用PlaceHolder动态加载用户控件,为何aspx页面再次提交后用户控件消失?

2013年12月24日 ⁄ 综合 ⁄ 共 901字 ⁄ 字号 评论关闭

用户控件:uc.ascx(内含按钮AscxButton)  
  页面文件:a.aspx(内含按钮AspxButton)  
  流程是这样的:  
  在a.aspx中点击按钮AspxButton,用户控件能够出现在页面上(如果一直未点击过就不出现),其事件代码为:  
  AspxButton_Click()  
  {  
          UC   uc   =   LoadControl("uc.ascx");  
          PlaceHolder.Controls.Add(uc);  
  }  
   
  这时用户控件能够准确出现在页面上。当我再按uc.ascx里的AscxButton后,则控件在页面消失。  
  我在a.aspx的Page_Load()中没写任何代码。  
   
  请问如何避免这种情况?我最终是希望即使点击AscxButton后页面上依然能够看到用户控件。 问题点数:50、回复次数:1Top

1 楼saucer(思归)回复于 2005-02-24 01:53:13 得分 50

you   need   to   remember   to   re-load   the   control   upon   postback  
   
   
  public   bool   Loaded  
  {  
      get   {   object   o   =   ViewState["Loaded"];  
  if   (o   ==   null)   return   false;   else   return   (bool)o;   }  
      set   {     ViewState["Loaded"]   =   value;}  
  }  
   
   
  void   LoadYourControl()  
  {  
          UC   uc   =   LoadControl("uc.ascx");  
          PlaceHolder1.Controls.Add(uc);  
  }  
   
  AspxButton_Click()  
  {  
      LoadYourControl();  
      Loaded   =   true;  
  }  
   
  void   Page_Load(object   sender,   EventArgs   e)  
  {  
    if   (Loaded)  
        LoadYourControl();  
  }  
 
  

抱歉!评论已关闭.