C# 使用Build Manager类加载ASPX文件并填充其控件

C# 使用Build Manager类加载ASPX文件并填充其控件,c#,asp.net,dynamic,webforms,C#,Asp.net,Dynamic,Webforms,我使用BuildManager类加载动态生成的ASPX文件,请注意它没有相应的.cs文件 使用下面的代码,我可以加载aspx文件,甚至可以在动态创建的aspx文件的控件集合中循环,但是当我给控件赋值时,它们不会显示出来。例如,如果我将值“Dummy”绑定到aspx页面的TextBox控件,则TextBox将保持为空 这是我正在使用的代码 protected void Page_Load(object sender, EventArgs e) { LoadPage("~/D

我使用BuildManager类加载动态生成的ASPX文件,请注意它没有相应的.cs文件

使用下面的代码,我可以加载aspx文件,甚至可以在动态创建的aspx文件的控件集合中循环,但是当我给控件赋值时,它们不会显示出来。例如,如果我将值“Dummy”绑定到aspx页面的TextBox控件,则TextBox将保持为空

这是我正在使用的代码

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(); } } } } } } } 受保护的无效页面加载(对象发送方、事件参数e) { 加载页(“~/Demo.aspx”); } 公共静态void加载页(字符串页面路径) { //获取引用路径的编译类型 Type=BuildManager.GetCompiledType(页面路径); //如果类型为null,则无法确定页面类型 if(type==null) 抛出新的ApplicationException(“页面”+pagePath+“未找到”); //强制转换页面对象(也可以强制转换接口实例) //在本例中,ASP2220Page是一个自定义的基本页 System.Web.UI.Page pageView=(System.Web.UI.Page)Activator.CreateInstance(类型); //呼叫页面标题 pageView.Title=“动态加载的页面…”; //调用ASP220Page的自定义属性 //pageView.InternalControls.Add( //新的LiteralControl(“
动态服务…”); //使用更新的对象处理请求 ((IHttpHandler)pageView.ProcessRequest(HttpContext.Current); LoadDataInDynamicPage(页面视图); } 专用静态无效加载数据动态页面(第prvPage页) { foreach(prvPage.Controls中的控件ctrl) { //查找表单控件 如果(ctrl.ID!=null) { if(ctrl.ID.Equals(“form1”)) { AllFormsClass cls=新的AllFormsClass(); 数据集ds=cls.GetConditionalData(“1”); foreach(ctrl.Controls中的控件中心) { 如果(ctr是文本框) { 如果(ctr.ID.Contains(“\M”)) { TextBox drpControl=(TextBox)ctr; drpControl.Text=ds.Tables[0]。行[0][ctr.ID]。ToString(); } 否则,如果(中心ID包含(“\O”)) { TextBox drpControl=(TextBox)ctr; drpControl.Text=ds.Tables[1]。行[0][ctr.ID]。ToString(); } } } } } } }
我看到你的部分代码来自。请阅读Mike的评论

倒过来:

((IHttpHandler)pageView).ProcessRequest(HttpContext.Current);
LoadDataInDynamicPage(pageView);
为此:

LoadDataInDynamicPage(pageView);
((IHttpHandler)pageView).ProcessRequest(HttpContext.Current);
在这种情况下,我认为改变调用顺序确实会改变最终结果。与之相反的。:)