views:

413

answers:

4

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:

A: 

Server.Transfer?

A: 

Whenever I've done something like this, I've always had to use Page.FindControl or 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).

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; 
}
Robert Harvey
A: 

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.

What's the reason you want to access the control collection of an executed page?

mnero0429
A: 

Did someone ever get a resolution to the original question?

AM