ASP.Net 2.0 - Retrieve list of controls in place holder

J

JimCinLA

I use a place holder to load different user controls on an .aspx page.

Let's call this default.aspx.

Each user controls contains about a dozen different ASP.Net controls.

My question is, how can I retrieve the list of controls from
default.aspx?
 
O

Olaf Rabbachin

Hi,
I use a place holder to load different user controls on an .aspx page.

Let's call this default.aspx.

Each user controls contains about a dozen different ASP.Net controls.

My question is, how can I retrieve the list of controls from
default.aspx?

the controls-collection applies to all controls. That is, if you have i.e.
a panel, you can loop through all its contained controls using i.e.
for i as integer=0 to pnlYourPanel.controls.count-1
response.write pnlYourPanel.controls(i).id
next
(A for-each is possible as well.)

Cheers,
Olaf
 
J

JimCinLA

Olaf said:
Hi,


the controls-collection applies to all controls. That is, if you have i.e.
a panel, you can loop through all its contained controls using i.e.
for i as integer=0 to pnlYourPanel.controls.count-1
response.write pnlYourPanel.controls(i).id
next
(A for-each is possible as well.)

Cheers,
Olaf

Olaf,

Thanks, but I really didn't word the question quite correctly.

My user control contains about 45 ASP.Net controls such as labels,
buttons, and drop down lists. (This doesn't include the list items
within the DDLs which are also ASP.Net controls.)

In default.aspx, I want to find all the controls that have been loaded
into the placeholder. But for some reason, I can only list about
a third of them using the following code...

ctl_1 = Page.LoadControl("~/UserControls/MyControl.ascx");
plcHol1.Controls.Add (ctl_1);

// ...

foreach (Control ctl in ctl_1.Controls)
Response.Write(ctl_1.ID + "<br>");

Every control has an ID.

But I can still find a specific control which doesn't appear using
the above by using refraction. That is,

if (ctl_1.FindControl("lblSomeLabel") != null)
Write.Response ("lblSomeLabel exists.")

What I think is happening is that controls that are contained
in tables nested inside tables don't show in the Controls
collection, but can be "smoked" out using refraction.
Of course I don't want to resort to this, because it's
slow, and I have to run this process many times.

Any ideas? Or does anyone see something obvious that
I'm overlooking?
 
F

Flinky Wisty Pomm

If you want to find nested controls, use a nested loop.

protected void ListAllControls(Control root, int level)
{
Response.Write(string.Format("{0}{1}<br />", root.ID, new string(' ',
level * 3)));
foreach(Control child in root.Controls)
{
ListAllControls(child, level + 1);
}
}



ListAllControls(myPanel, 0) should then give you

myPanel
mySubControl1
mySubSubControl
myOtherSubSubControl
mySubControl2
...
 

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

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top