Dynamically Load UserControl

Last post 07-10-2007 10:21 AM by AndrewSeven. 1 replies.

Sort Posts:

  • Dynamically Load UserControl

    07-09-2007, 4:50 PM
    • Member
      312 point Member
    • officialboss
    • Member since 09-13-2006, 8:15 PM
    • California
    • Posts 168

    Hello Folks,

    I couldn't find an appropriate solution on these forums for my situation hence I am creating a new post.

    My setup: MasterPage has UpdatePanel, Default.aspx is wired to the Master page, Default.aspx page has Ajax Tab container. I load a UserControl in one of the Tabs on Default.aspx page, works great. But when I try to add something a PlaceHolder in the UserControl on Page_Load of the UC, I get "Exception has been thrown by the target of an invocation.". Also when I step through it seems that the UserControl already is postback and if (!Page.IsPostBack) does not fire.

    Default.aspx 

      protected void btnContinue_Click(object sender, EventArgs e)
        {
            Control ctrl = LoadControl("~/Forms/myControl.ascx", tbCustNumber.Text);
            ctrl.ID = "ucCust";
            
            // Initialize Tab
            TabContainer tc = (TabContainer)CommonUtility.FindControlRecursive(this, "tcDefault");
            TabPanel tab = (TabPanel)tc.FindControl("tabCust");// new TabPanel();
            tab.Controls.Add(ctrl);        
        }
     
    private UserControl LoadControl(string UserControlPath, params object[] constructorParameters)
        {
            System.Collections.Generic.List constParamTypes = new System.Collections.Generic.List();
            foreach (object constParam in constructorParameters)
            {
                constParamTypes.Add(constParam.GetType());
            }
    
            UserControl ctl = Page.LoadControl(UserControlPath) as UserControl;
    
            // Find the relevant constructor
            System.Reflection.ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray());
    
            //And then call the relevant constructor
            if (constructor == null)
            {
                throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType.ToString());
            }
            else
            {
                try
                {
                    constructor.Invoke(ctl, constructorParameters);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    
            // Finally return the fully initialized UC
            return ctl;
        }
    myControl.ascx  
    1        private string _custNumber;
    2        public string CustNumber
    3        {
    4            get { return _custNumber; }
    5            set { _custNumber = value; }
    6        }
    7    	
    8    	public Forms_Loan() { }
    9    
    10       public Forms_Loan(string custNumber)
    11       {
    12           CustNumber = custNumber; // Add to Control Property
    13           LoadCustomer(custNumber);
    14       }
    15   	
    16   	protected void Page_Load(object sender, EventArgs e)
    17       {
    18           if (!Page.IsPostBack)
    19               LoadCustomer(custNumber);
    20       }
    21   	
    22   	private void LoadCustomer(string custNumber)
    23       {
    24           // Bind DataGrid with customer information
    25   	}
    
     Line 19 above does not fire. Also in the LoadCustomer method if I do:

    PlaceHolder plh = Page.FindControl("plhLoan") as PlaceHolder;
    plh.Controls.Add("something");

    I get the same error.

    Any help would be appreciated in trying to get my dynamic user controls working. Perhaps I need a better understanding with examples on how this all works.

    Please be sure to click "Mark as Answer" on the post that helped you.
  • Re: Dynamically Load UserControl

    07-10-2007, 10:21 AM
    Answer
    • Contributor
      2,184 point Contributor
    • AndrewSeven
    • Member since 02-06-2004, 2:05 PM
    • Posts 437

    "when I step through it seems that the UserControl already is postback"

    Seems to make sense to be in postback, it is happening in btnContinue_Click...

    I would suggest using properties rather than that construtor trick.

Page 1 of 1 (2 items)