I'm trying to make a usercontrol work like a plugin: load it dynamically (using reflection) from a user's selection. After I click the button, I can see that the UI had adjusted to supposedly indicate that the user control has been loaded but I cannot
the control itself. I even used viewstate but still I cannot see the control.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
LoadUserControl();
//(a)I also tried to put in a ViewState but still nothing happens.
//if (ViewState["UserControl"] != null)
//{
// UserControl uc = (UserControl)ViewState["UserControl"];
// pnlReportControl.Controls.Add(LoadControl());
//}
}
//supposedly done after a button click
private void LoadUserControl()
{
enrolmentReports = string.Concat(Server.MapPath("~"), enrolmentDll);
assembly = Assembly.LoadFrom(enrolmentReports);
Type type = assembly.GetType("TZEnrollmentReports.EnrollmentUserControl");
//also tried this way but also didn't work
//Page.LoadControl(type, null);
UserControl uc1 = (UserControl)LoadControl(type, null);
pnlReportControl.Controls.Add(uc1);
//(a)
//ViewState["UserControl"] = uc1;
}
Please help. This is just the first step of the whole complicated process. I still have to get a dataset from that report. But I think I'm leaving that to another thread.
iceheaven31
Member
2 Points
2 Posts
Dynamically loaded user control not rendering
May 16, 2012 10:11 AM|LINK
I'm trying to make a usercontrol work like a plugin: load it dynamically (using reflection) from a user's selection. After I click the button, I can see that the UI had adjusted to supposedly indicate that the user control has been loaded but I cannot the control itself. I even used viewstate but still I cannot see the control.
protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) LoadUserControl(); //(a)I also tried to put in a ViewState but still nothing happens. //if (ViewState["UserControl"] != null) //{ // UserControl uc = (UserControl)ViewState["UserControl"]; // pnlReportControl.Controls.Add(LoadControl()); //} } //supposedly done after a button click private void LoadUserControl() { enrolmentReports = string.Concat(Server.MapPath("~"), enrolmentDll); assembly = Assembly.LoadFrom(enrolmentReports); Type type = assembly.GetType("TZEnrollmentReports.EnrollmentUserControl"); //also tried this way but also didn't work //Page.LoadControl(type, null); UserControl uc1 = (UserControl)LoadControl(type, null); pnlReportControl.Controls.Add(uc1); //(a) //ViewState["UserControl"] = uc1; }Please help. This is just the first step of the whole complicated process. I still have to get a dataset from that report. But I think I'm leaving that to another thread.
Thank you!
Dynamic usercontol reflection
_Manvel_
Contributor
4240 Points
922 Posts
Re: Dynamically loaded user control not rendering
May 16, 2012 11:10 AM|LINK
Just to clarify, your control has static content ? Or you need to bind it to some datasource, in order to have some content ?
Dynamic usercontol reflection
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Dynamically loaded user control not rendering
May 18, 2012 01:20 AM|LINK
Hello:)
Please put your controls in the OnInit function instead of Page_Load event:
public partial class WebForm1 : System.Web.UI.Page { Control c = null; protected override void OnInit(EventArgs e) { c = LoadControl("~/MyControl1.ascx"); base.OnInit(e); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Response.Write(c); } } }