我要在页面中动态加载一些用户控件,每个用户控件都是一个独立的逻辑单元,我给他们每一个都增加属性便于对外访问,
比如:
public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.strSql = this.textbox1.text + this.text2.text + ... ;
        }        /// <summary>
        /// 公共成员
        /// </summary>
        public string strSql;
    }把他们加载在页面的placeholder中,
 protected void Page_Load(object sender, EventArgs e)
{
   if(!this.ispostback)
   {  
      this.LoadControl("WebUserControl1.ascx")
      this.LoadControl("WebUserControl2.ascx")
      this.LoadControl("WebUserControl3.ascx")
   }
   else
  {
      //怎样在页面回发的时候取得控件的公共成员的值呢,this.placeholder.controls.count = 0
   }
}
怎么取呢

解决方案 »

  1.   


    //在MyControl.ascx用户控件里 建一个属性
    public string UserName
        {
    //可改成其他控件
            get { return TextBox1.Text; }
            set { TextBox1.Text = value; }
        }//在使用页面重写OnInit方法protected override void OnInit(EventArgs e)
        {
            MyControl control = (MyControl)this.LoadControl("MyControl.ascx");
            control.ID = "myC";
            form1.Controls.Add(control);//from1可改成其他控件
        }//获取
    MyControl con = (MyControl)form1.FindControl("myC");
            if (con != null)
            {
                Response.Write(con.UserName);
            }
      

  2.   

    我的代码:用户控件
    public partial class WebUserControl1 : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (this.IsPostBack)
                    this.s = this.tb.Text;
            }        private string _s;        public string s
            {
                get { return this._s; }
                set { this._s = value; }
            }
        }调用页:
     public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (this.IsPostBack)
                {
                    WebUserControl1 wu = (WebUserControl1)this.ph.FindControl("symbol");
                    string sss = wu.s;
                }
            }        protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);            WebUserControl1 wu = (WebUserControl1)this.LoadControl("WebUserControl1.ascx");            wu.ID = "symbol";            this.ph.Controls.Add(wu);
            }
        }
      

  3.   

    public string UserName
    {
     get{string.IsNullOrEmpty(ViewState["UserName"].ToString())?"":ViewState["UserName"].ToString();}
    set{ViewState["UserName"]=value;}
    }
      

  4.   

    还有我想问,为什么在page_load中loadControls取不到,在init中就可以呢??