假设我有两个网络表单, A.aspx 和 B.aspx ,其中B.aspx包含一些简单的网页控件,例如文本框和按钮。< / p>
我正在尝试执行以下操作:
当要求A.aspx时, 我想动态调用并将B.aspx加载到内存中并输出B.aspx中包含的所有控件的详细信息。
以下是我在A.aspx的代码隐藏中尝试的内容:
var compiledType = BuildManager.GetCompiledType("~/b.aspx");
if (compiledType != null)
{
var pageB = (Page)Activator.CreateInstance(compiledType);
}
foreach (var control in pageB.Controls)
{
//output some details for each control, like it's name and type...
}
当我尝试上面的代码时, pageB 的控件集合始终为空。
关于如何让它发挥作用的任何想法?
其他一些重要细节:
答案 0 :(得分:0)
每当我做完这样的事情时,我总是不得不使用Page.FindControl or Control.FindControl.
不幸的是,没有FindControl的递归版本。但是,this blog post提供了FindControlRecursive函数。有了它,您可以迭代容器中的所有控件(包括,可能是您的PageB)。
private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
答案 1 :(得分:0)
我真的认为你应该提供更多关于你要做什么的背景知识。 HttpServerUtility :: Execute(String,TextWriter)将执行一个页面并将结果写入提供的TextWriter对象。从那里,您可以检查页面的内容,如果生成的HTML文档格式正确,您甚至可以通过.NET XML API遍历其结构。
您想要访问已执行页面的控件集合的原因是什么?