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. Advertisements

  2. Could you, for instance, use the Page.Controls collection and recursively
    walk through each control?
     
    Peter Rilling, Nov 21, 2005
    #2
    1. Advertisements

  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
     
    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
     
    J, Nov 21, 2005
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.