Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Build Manager Class to Load ASPX Files and Populate its Controls

I am using BuildManager Class to Load a dynamically generated ASPX File, please note that it does not have a corresponding .cs file.

Using Following code I am able to load the aspx file, I am even able to loop through the control collection of the dynamically created aspx file, but when I am assigning values to controls they are not showing it up. for example if I am binding the value "Dummy" to TextBox control of the aspx page, the textbox remains empty.

Here's the code that I am using


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(); } } } } } } }
like image 649
Sandhurst Avatar asked Mar 21 '10 17:03

Sandhurst


1 Answers

I saw that you got part of your code from How To Dynamically Load A Page For Processing. Read the comments too as this one by Mike.

Invert this:

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

To this:

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

In this case changing the order of the calls does change the end result I think. The inverse of Commutativity property. :)

like image 142
Leniel Maccaferri Avatar answered Oct 31 '22 21:10

Leniel Maccaferri