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

Discussion in 'ASP .Net' started by James Doran, Jun 15, 2004.

  1. James Doran

    James Doran Guest

    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
     
    James Doran, Jun 15, 2004
    #1
    1. Advertising

  2. "James Doran" <> wrote in message
    news:...
    > 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.
    --
    John Saunders
    johnwsaundersiii at hotmail
     
    John Saunders, Jun 15, 2004
    #2
    1. Advertising

  3. James Doran

    James Doran Guest

    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

    "John Saunders" <> wrote in message
    news:...
    > "James Doran" <> wrote in message
    > news:...
    > > 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.
    > --
    > John Saunders
    > johnwsaundersiii at hotmail
    >
    >
     
    James Doran, Jun 15, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Doran
    Replies:
    0
    Views:
    379
    James Doran
    Jun 18, 2004
  2. Gogo
    Replies:
    1
    Views:
    2,105
    Sudsy
    Sep 4, 2003
  3. runescience
    Replies:
    0
    Views:
    1,462
    runescience
    Feb 9, 2006
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    981
    Øyvind Isaksen
    May 18, 2007
  5. =?Utf-8?B?SXJ3YW5zeWFo?=
    Replies:
    4
    Views:
    2,463
    =?Utf-8?B?SXJ3YW5zeWFo?=
    Oct 30, 2007
Loading...

Share This Page