Iterate every Page Page.Controls collection in a project at design-time

J

James Doran

Hello,

I'd like to iterate through each Page of my ASP.NET project from
within a Custom web control and access the Page.Controls collection.

I've tried using Reflection on the web project assembly, but the
controls collection count was zero. While this method gives details
of the private controls in each page, I need access to actual
instances of the controls to interrogate their individual properties.

Next I tried from with a web custom control. Using a custom
UITypeEditor, I was able to access the page object in the EditValue
method of the type editor using the following code:

////////////////////////////////////////////////////////////////////////
System.Web.UI.Page oCurrentPage = null;
System.Collections.IEnumerator oEn;

// Use context parameter to determine page
oEn = ((System.Web.UI.WebControls.WebControl)
context.Instance).Site.Container.Components.GetEnumerator();

// Get the page that the control is situated on...
oEn.MoveNext();
oCurrentPage = (System.Web.UI.Page)oEn.Current;
////////////////////////////////////////////////////////////////////////

This was a step in the right direction, as I was able to get access to
the controls collection. The only problem is that I can't access
other pages of the same project from within this control.

The only other thing I can think of is using ENVDTE to go through the
project files, but I don't think I would have the same amount of
control and would end up having to manually parse the ASPX files,
which would be very messy.

I would be most grateful if anyone has time to help.

Many thanks,

James
 
J

John Saunders

James Doran said:
Hello,

I'd like to iterate through each Page of my ASP.NET project from
within a Custom web control and access the Page.Controls collection.

I've tried using Reflection on the web project assembly, but the
controls collection count was zero. While this method gives details
of the private controls in each page, I need access to actual
instances of the controls to interrogate their individual properties.

Next I tried from with a web custom control. Using a custom
UITypeEditor, I was able to access the page object in the EditValue
method of the type editor using the following code:

////////////////////////////////////////////////////////////////////////
System.Web.UI.Page oCurrentPage = null;
System.Collections.IEnumerator oEn;

// Use context parameter to determine page
oEn = ((System.Web.UI.WebControls.WebControl)
context.Instance).Site.Container.Components.GetEnumerator();

// Get the page that the control is situated on...
oEn.MoveNext();
oCurrentPage = (System.Web.UI.Page)oEn.Current;
////////////////////////////////////////////////////////////////////////

This was a step in the right direction, as I was able to get access to
the controls collection. The only problem is that I can't access
other pages of the same project from within this control.

The only other thing I can think of is using ENVDTE to go through the
project files, but I don't think I would have the same amount of
control and would end up having to manually parse the ASPX files,
which would be very messy.

I would be most grateful if anyone has time to help.

I think you really don't want to do this. How often do you expect your
iterate code to be called? Every time a page is added to the project?
Removed? Every time a control is added, removed or changed? That's a lot of
calls.

That's also assuming that you can find a way to do this at all. You would
have to use the VS.NET extension API (see "
Extending the Visual Studio Environment" in the VS.NET documentation) to
locate each file. Then, you'd somehow have to create a designer for each
page (I don't believe a designer can reference more than a single page) and
have that designer iterate through the controls for you.

Perhaps you should consider a different way of meeting the underlying
requirement.
 
J

James Doran

John,

Many thanks for your reply.

It sounds like quite a job, and like you say quite inefficient! I'll have a
look into the documentation you've suggested though.

Cheers,

James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top