How to access controls collection of dynamically loaded aspx page?
Posted
by Naasir
on Stack Overflow
See other posts from Stack Overflow
or by Naasir
Published on 2009-06-27T00:36:22Z
Indexed on
2010/04/06
14:03 UTC
Read the original article
Hit count: 167
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.
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.
Here is what I tried in the codebehind for 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.
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.
© Stack Overflow or respective owner