动态加载控件的问题。

一个UserControl类型的MainControl,其中放置了一个Panel1,一个Button
另外还有几个子UserControl,我的想法是:

在页面Load的时候,通过LoadControl加载一个UserControl到Pannel1中,用户通过button返回时,先在LoadViewState中动态生成上次的UserControl,接着在调用base的LoadControl,然后处理button的操作,加载一个新的UserControl(不加载也一样),结果报告说LoadViewState失败,因为Control Tree不一致,跟踪进控件树,发现确实这样,于是在MainControl中增加了一个什么都不做的UserControl1,做Container用,把动态加载的UserControl放到userControl1中,结果还是一样,只要禁止base.LoadViewState就没有问题。

使用由于需要使用动态加载的UserControl中用户输入的信息,因此不能禁止base.LoadViewState,但使用后面的方法,对base.LoadViewState进行异常捕获,错误依旧,但已经可以获得动态加载的userControl的用户输入数据了。

从这个问题来看,动态加载的控件是已经成功LoadViewState了,错误应该是MainControl某个地方出现问题,头烂了,还是没有办法解决。

CS代码:

namespace SuperData.SDInfo.bCenter.Web.CN.CorpInfo
{
 using System.Collections;
 using System;
 using System.Data;
 using System.Drawing;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Web.UI.HtmlControls;

 /// <summary>
 ///  YellowPageAdd 的摘要说明。
 /// </summary>
 public class YellowPageAdd : System.Web.UI.UserControl
 {
  protected System.Web.UI.WebControls.Button btnNextStep;
  protected System.Web.UI.WebControls.Button btnPreviouStep;

  private int m_iCurStep=0;
  protected System.Web.UI.WebControls.Label lblStep0;
  protected System.Web.UI.WebControls.Label lblStep1;
  protected System.Web.UI.WebControls.Label lblStep2;
  protected System.Web.UI.WebControls.Label lblStep3;
  protected System.Web.UI.WebControls.Label lblStep4;
  protected System.Web.UI.HtmlControls.HtmlInputHidden hiddenCurStep;

  private UserControl m_UserControl;
  protected UserControl EmptyContainerUserControl1;

  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!this.IsPostBack)
   {
    this.LoadMainControl();
   }
  }

  protected override void LoadViewState(object savedState)
  {
   ArrayList arrViewState=savedState as ArrayList;
   this.m_iCurStep=(int)arrViewState[0];
   this.LoadMainControl();
   try
   {
    base.LoadViewState (savedState);
   }
   catch
   {
    Response.Write("error");
   }
  }
  protected override object SaveViewState()
  {
   ArrayList arrViewState=new ArrayList();
   arrViewState.Add(this.m_iCurStep);
   arrViewState.Add(base.SaveViewState());
   return arrViewState;
  }


  private void LoadMainControl()
  {
   this.EmptyContainerUserControl1.Controls.Clear();
   switch(this.m_iCurStep)
   {
     //条款
    case 0:
     this.m_UserControl=this.LoadControl("~/CmnAscxs/LicenseCtrl.Ascx") as UserControl;
     (this.m_UserControl as CmnAscxs.LicenseCtrl).Title="黄页注册条款";
     (this.m_UserControl as CmnAscxs.LicenseCtrl).Description="请仔细阅读条款,点击下一步表示您同意本条款的各项说明";
     break;
     //注册ID
    case 1:
     this.m_UserControl=this.LoadControl("~/CmnAscxs/CN/AccessMobileID.Ascx") as UserControl;
     break;
     //企业信息
    case 2:
     this.m_UserControl=this.LoadControl("~/AscxCtrls/CN/PublicCorpInfo.Ascx") as UserControl;
     break;
     //确认
    case 3:
     this.m_UserControl=this.LoadControl("~/AscxCtrls/CN/PublicCorpShow.Ascx") as UserControl;
     break;
     //完成
    case 4:
     this.m_UserControl=this.LoadControl("~/CN/ErrorPage.Ascx") as UserControl;
     break;
   }
   this.EmptyContainerUserControl1.Controls.Add(this.m_UserControl);
  }


  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  ///  设计器支持所需的方法 - 不要使用代码编辑器
  ///  修改此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.btnPreviouStep.Click += new System.EventHandler(this.btnPreviouStep_Click);
   this.btnNextStep.Click += new System.EventHandler(this.btnNextStep_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void btnNextStep_Click(object sender, System.EventArgs e)
  {
   if(this.m_iCurStep==1)
    Response.Write("ID:"+(this.m_UserControl as CmnAscxs.CN.AccessMobileID).MobileID);
   if(this.m_iCurStep<4)
   {
    this.m_iCurStep++;
    this.m_UserControl.Dispose();
    this.LoadMainControl();
   }
  }

  private void btnPreviouStep_Click(object sender, System.EventArgs e)
  {
   this.m_iCurStep--;
   this.m_UserControl.Dispose();
   this.LoadMainControl();
  }

  protected override void OnPreRender(EventArgs e)
  {
   this.btnNextStep.Text="下一步";
   switch(this.m_iCurStep)
   {
    case 0:
     this.btnPreviouStep.Enabled=false;
     break;
    case 1:
     this.btnPreviouStep.Enabled=false;
     break;
    case 2:
     this.btnPreviouStep.Enabled=true;
     break;
    case 3:
     this.btnPreviouStep.Enabled=true;
     this.btnNextStep.Text="保存";
     break;
    case 4:
     this.btnPreviouStep.Enabled=false;
     this.btnNextStep.Text="完成";
     break;
   }
   (FindControl("lblStep"+this.m_iCurStep.ToString()) as Label).ForeColor=Color.Red;
   this.hiddenCurStep.Value=this.m_iCurStep.ToString();
   base.OnPreRender (e);
  }

 }
}

posted @ 2004-07-12 18:55  无之无  阅读(874)  评论(0编辑  收藏