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.