Web .NET Getting a list of page controls from the server side

In my web page, there is a list of controls that belong to the same class.  How can I get a list/IEnumerable of them from the server side?
Mare22Asked:
Who is Participating?
 
Ramkisan JagtapConnect With a Mentor Lead DeveloperCommented:
Hi,
You can get the list of controls using Page.Controls property. You can collect all the controls with your class using their CssClass Property.

Regards,
Ram
0
 
Luis PérezConnect With a Mentor Software Architect in .NetCommented:
You can do a recursive function, because you can start in Page.Controls, but don't forget that a control can contain another controls:

As you didn't tell what server language you're using, I'll write in VB. If you prefer C#, please telll me and I'll translate.

'Declare list at page-level
Private m_Controls As List(Of Control) = New List(Of Control)

Private Sub CheckControls(ByVal col As ControlCollection)
    For Each c As Control In col
        'Check if c is suitable to be in your m_Controls List
        'I don't know what you wanted to say with "belong to the same class". If you were trying
        'to say that the controls have the same style class, you can check the CssClass property.
        'If you were talking about the controls base class, check c.GetType.ToString

        'Once you have checked for this concrete control (c), look for its subcontrols (if available)...
        Call CheckControls(c.Controls)
    Next
End Sub

Hope that helps.
0
 
Luis PérezSoftware Architect in .NetCommented:
Oh, I forgot that in Page_Load you must call for the first time to CheckControls:

Page_Load:
Call CheckControls(Me.Controls)

HTH
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.