我試圖做一個像插件一樣的usercontrol工作:從用戶的選擇動態加載它(使用反射)。當我點擊按鈕後,我可以看到UI已經調整,以表明用戶控件已被加載,但我不能控制自己。我甚至使用了viewstate,但仍然看不到控件。動態加載用戶控件而不是渲染
請在下面找到我的代碼:
protected void Page_Load(object sender, EventArgs e)
{
//the scenario should be: a user clicking a button and from there,
//loads the control just below it, but still in the same page.
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");
enrolmentMethodInfos = type.GetMethods();
//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;
}
請幫助。這只是整個複雜過程的第一步。我仍然需要從該報告中獲取數據集。但我想我會把它留給另一個線程。
謝謝!
如果您嘗試調試你的代碼,是'uc1'正確實例化?它是您希望顯示的用戶控件嗎? – themarcuz
其實是的。當我嘗試調試代碼時,它顯示了我期望的usercontrol和其中的所有方法。 UI甚至改變以適應用戶控制的假定高度。 – iceheaven31
你確定問題不在usercontrol本身上嗎?也許它不能正確生成html。你是否試圖以硬編碼的方式將它加載到頁面上(所以不要動態加載),以驗證它是否正確顯示? – themarcuz