2010-03-21 4 views
6

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(); } } } } } } }

Trả lời

4

tôi thấy rằng bạn có một phần của mã của bạn từ How To Dynamically Load A Page For Processing. Đọc các nhận xét quá như thế này one bởi Mike.

Invert này:

((IHttpHandler)pageView).ProcessRequest(HttpContext.Current); 
LoadDataInDynamicPage(pageView); 

Để này:

LoadDataInDynamicPage(pageView); 
((IHttpHandler)pageView).ProcessRequest(HttpContext.Current); 

Trong trường hợp này thay đổi thứ tự của các cuộc gọi không thay đổi kết quả cuối cùng tôi nghĩ. Nghịch đảo của Commutativity property. :)