Let's say I have two webforms, A.aspx and B.aspx, where B.aspx contains some simple web controls such as a textbox and a button.
假设我有两个webforms,A.aspx和B.aspx,其中B.aspx包含一些简单的Web控件,如文本框和按钮。
I'm trying to do the following:
我正在尝试执行以下操作:
When A.aspx is requested, I want to dynamically call and load B.aspx into memory and output details of all the controls contained in B.aspx.
当请求A.aspx时,我想动态调用并将B.aspx加载到内存中并输出B.aspx中包含的所有控件的详细信息。
Here is what I tried in the codebehind for A.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...
}
When I try the code above, the controls collection for pageB is always empty.
当我尝试上面的代码时,pageB的控件集合始终为空。
Any ideas on how I can get this to work?
关于如何让这个工作的任何想法?
Some other important details:
其他一些重要细节:
- both webforms utilize a master page (so the web controls in b.aspx are actually placed within a "content" tag)
- I've also tried using BuildManager.CreateInstanceFromVirtualPath. No luck.
两个webforms都使用母版页(因此b.aspx中的web控件实际上放在“content”标签内)
我也尝试过使用BuildManager.CreateInstanceFromVirtualPath。没运气。
2 个解决方案
#1
I really think you should provide more background on what you're trying to do. HttpServerUtility::Execute(String, TextWriter) will execute a page and write the result to the provided TextWriter object. From there you can examine the contents of the page, and if the resulting HTML document is well-formed, you can even traverse its structure via the .NET XML APIs.
我真的认为你应该提供更多关于你想要做的事情的背景知识。 HttpServerUtility :: Execute(String,TextWriter)将执行一个页面并将结果写入提供的TextWriter对象。从那里,您可以检查页面的内容,如果生成的HTML文档格式正确,您甚至可以通过.NET XML API遍历其结构。
What's the reason you want to access the control collection of an executed page?
您想要访问已执行页面的控件集合的原因是什么?
#2
Whenever I've done something like this, I've always had to use Page.FindControl or Control.FindControl.
每当我做完这样的事情时,我总是不得不使用Page.FindControl或Control.FindControl。
Unfortunately, there is no recursive version of FindControl. However, this blog post offers a FindControlRecursive function. With it, you can iterate over all of the controls in a container (including, presumably, your PageB).
不幸的是,没有FindControl的递归版本。但是,此博客文章提供了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
I really think you should provide more background on what you're trying to do. HttpServerUtility::Execute(String, TextWriter) will execute a page and write the result to the provided TextWriter object. From there you can examine the contents of the page, and if the resulting HTML document is well-formed, you can even traverse its structure via the .NET XML APIs.
我真的认为你应该提供更多关于你想要做的事情的背景知识。 HttpServerUtility :: Execute(String,TextWriter)将执行一个页面并将结果写入提供的TextWriter对象。从那里,您可以检查页面的内容,如果生成的HTML文档格式正确,您甚至可以通过.NET XML API遍历其结构。
What's the reason you want to access the control collection of an executed page?
您想要访问已执行页面的控件集合的原因是什么?
#2
Whenever I've done something like this, I've always had to use Page.FindControl or Control.FindControl.
每当我做完这样的事情时,我总是不得不使用Page.FindControl或Control.FindControl。
Unfortunately, there is no recursive version of FindControl. However, this blog post offers a FindControlRecursive function. With it, you can iterate over all of the controls in a container (including, presumably, your PageB).
不幸的是,没有FindControl的递归版本。但是,此博客文章提供了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;
}