2010-03-21 22 views
6

Estoy usando la clase BuildManager para cargar un archivo ASPX generado dinámicamente, tenga en cuenta que no tiene un archivo .cs correspondiente.Uso de la clase Build Manager para cargar archivos ASPX y llenar sus controles

Usando el siguiente código Puedo cargar el archivo aspx, incluso puedo recorrer la colección de control del archivo aspx creado dinámicamente, pero cuando estoy asignando valores a los controles, no lo muestran. por ejemplo, si estoy vinculando el valor "Dummy" al control TextBox de la página aspx, el cuadro de texto permanece vacío.

Aquí está el código que estoy usando

 

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

Respuesta

4

vi que tiene parte de su código de How To Dynamically Load A Page For Processing. Lea los comentarios también como este one por Mike.

Invertir:

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

A esto:

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

En esta cambiando el orden de las llamadas no cambiará el resultado final creo caso. El inverso de Commutativity property. :)

Cuestiones relacionadas