Tôi đang sử dụng Lớp BuildManager để tải Tệp ASPX được tạo động, xin lưu ý rằng tệp này không có tệp .cs tương ứng.Sử dụng Lớp Trình quản lý Xây dựng để Tải các Tệp ASPX và Điền các Điều khiển của nó
Sử dụng mã sau Tôi có thể tải tệp aspx, thậm chí tôi có thể lặp qua bộ sưu tập kiểm soát của tệp aspx được tạo động, nhưng khi tôi gán giá trị cho các điều khiển mà chúng không hiển thị. ví dụ nếu tôi ràng buộc giá trị "Giả" để điều khiển TextBox của trang aspx, hộp văn bản vẫn trống.
Dưới đây là đoạn code mà tôi đang sử dụng
protected void Page_Load(object sender, EventArgs e) { LoadPage("~/Demo.aspx"); } public static void LoadPage(string pagePath) { // get the compiled type of referenced path Type type = BuildManager.GetCompiledType(pagePath); // if type is null, could not determine page type if (type == null) throw new ApplicationException("Page " + pagePath + " not found"); // cast page object (could also cast an interface instance as well) // in this example, ASP220Page is a custom base page System.Web.UI.Page pageView = (System.Web.UI.Page)Activator.CreateInstance(type); // call page title pageView.Title = "Dynamically loaded page..."; // call custom property of ASP220Page //pageView.InternalControls.Add( // new LiteralControl("
Served dynamically...")); // process the request with updated object ((IHttpHandler)pageView).ProcessRequest(HttpContext.Current); LoadDataInDynamicPage(pageView); } private static void LoadDataInDynamicPage(Page prvPage) { foreach (Control ctrl in prvPage.Controls) { //Find Form Control if (ctrl.ID != null) { if (ctrl.ID.Equals("form1")) { AllFormsClass cls = new AllFormsClass(); DataSet ds = cls.GetConditionalData("1"); foreach (Control ctr in ctrl.Controls) { if (ctr is TextBox) { if (ctr.ID.Contains("_M")) { TextBox drpControl = (TextBox)ctr; drpControl.Text = ds.Tables[0].Rows[0][ctr.ID].ToString(); } else if (ctr.ID.Contains("_O")) { TextBox drpControl = (TextBox)ctr; drpControl.Text = ds.Tables[1].Rows[0][ctr.ID].ToString(); } } } } } } }