Solved

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

Posted on 2011-09-20
3
212 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:Mare22
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 250 total points
ID: 36571807
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
 
LVL 25

Assisted Solution

by:Luis Pérez
Luis Pérez earned 250 total points
ID: 36572320
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
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 36572387
Oh, I forgot that in Page_Load you must call for the first time to CheckControls:

Page_Load:
Call CheckControls(Me.Controls)

HTH
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question