Programmaticly enumerate all controls and properties on an ASP.NET page

Discussion in 'ASP .Net' started by J, Nov 21, 2005.

  1. J

    J Guest

    I'm trying to make a list of certain properties of all the controls on an
    ASP.NET page. Does anyone know of an easy way to do this? I've tried to
    do it a couple of different ways but I'm not getting the hang of it I'm
    afraid. Right now I'm parsing text file but that's a pain, not very
    elegant, and I'm not sure it'll give me all the info I need. Right now
    I'm using VB.Net as the back end but I'll take C#.Net code in a pinch. :)

    Tanx in advance,

    J
     
    J, Nov 21, 2005
    #1
    1. Advertising

  2. Could you, for instance, use the Page.Controls collection and recursively
    walk through each control?

    "J" <> wrote in message
    news:p...
    > I'm trying to make a list of certain properties of all the controls on an
    > ASP.NET page. Does anyone know of an easy way to do this? I've tried to
    > do it a couple of different ways but I'm not getting the hang of it I'm
    > afraid. Right now I'm parsing text file but that's a pain, not very
    > elegant, and I'm not sure it'll give me all the info I need. Right now
    > I'm using VB.Net as the back end but I'll take C#.Net code in a pinch. :)
    >
    > Tanx in advance,
    >
    > J
    >
    >
     
    Peter Rilling, Nov 21, 2005
    #2
    1. Advertising

  3. J

    J Guest

    Hi Peter,

    I've tried something similar but it seems more complicated than that. If
    I look through the list of objects on the property list on the right hand
    side I see six objects (including the web form). When I use
    page.controls.count I get 3. Those objects are 'ctl0', 'form1', and
    'ctl1' respectively. It looks like I can access the controls on the form
    through 'form1'. Page.Controls(1).Controls.Count yields the correct
    number. I suspect that means that I first have to iterate through the
    page controls and find out which one is the form. Is that always the way
    it works? If not then this could get messy really quickly. :(

    J

    On Sun, 20 Nov 2005 21:26:32 -0800, Peter Rilling wrote:

    > Could you, for instance, use the Page.Controls collection and recursively
    > walk through each control?
    >
    > "J" <> wrote in message
    > news:p...
    >> I'm trying to make a list of certain properties of all the controls on an
    >> ASP.NET page. Does anyone know of an easy way to do this? I've tried to
    >> do it a couple of different ways but I'm not getting the hang of it I'm
    >> afraid. Right now I'm parsing text file but that's a pain, not very
    >> elegant, and I'm not sure it'll give me all the info I need. Right now
    >> I'm using VB.Net as the back end but I'll take C#.Net code in a pinch. :)
    >>
    >> Tanx in advance,
    >>
    >> J
    >>
    >>
     
    J, Nov 21, 2005
    #3
  4. J

    J Guest

    SWEET!

    Here's what I did.

    Private Sub CtrlList(ByVal ctrlCol As ControlCollection)
    Dim ctrl As Control
    For Each ctrl In ctrlCol
    If ctrl.HasControls Then
    CtrlList(ctrl.Controls)
    Else
    System.Diagnostics.Debug.WriteLine(ctrl.ClientID)
    End If
    Next
    End Sub

    I love recursion. ;)

    J

    On Mon, 21 Nov 2005 00:56:16 -0500, J wrote:

    > Hi Peter,
    >
    > I've tried something similar but it seems more complicated than that. If
    > I look through the list of objects on the property list on the right hand
    > side I see six objects (including the web form). When I use
    > page.controls.count I get 3. Those objects are 'ctl0', 'form1', and
    > 'ctl1' respectively. It looks like I can access the controls on the form
    > through 'form1'. Page.Controls(1).Controls.Count yields the correct
    > number. I suspect that means that I first have to iterate through the
    > page controls and find out which one is the form. Is that always the way
    > it works? If not then this could get messy really quickly. :(
    >
    > J
    >
    > On Sun, 20 Nov 2005 21:26:32 -0800, Peter Rilling wrote:
    >
    >> Could you, for instance, use the Page.Controls collection and recursively
    >> walk through each control?
    >>
    >> "J" <> wrote in message
    >> news:p...
    >>> I'm trying to make a list of certain properties of all the controls on an
    >>> ASP.NET page. Does anyone know of an easy way to do this? I've tried to
    >>> do it a couple of different ways but I'm not getting the hang of it I'm
    >>> afraid. Right now I'm parsing text file but that's a pain, not very
    >>> elegant, and I'm not sure it'll give me all the info I need. Right now
    >>> I'm using VB.Net as the back end but I'll take C#.Net code in a pinch. :)
    >>>
    >>> Tanx in advance,
    >>>
    >>> J
    >>>
    >>>
     
    J, Nov 21, 2005
    #4
    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. GS
    Replies:
    1
    Views:
    14,906
    Karl Seguin
    Jan 9, 2006
  2. LaptopHeaven
    Replies:
    0
    Views:
    411
    LaptopHeaven
    Oct 5, 2006
  3. Replies:
    0
    Views:
    279
  4. Angus
    Replies:
    10
    Views:
    206
    Angus
    Apr 16, 2007
  5. gyrm
    Replies:
    0
    Views:
    116
Loading...

Share This Page