Show/Hide Multiple User Controls on Page

V

vcuankitdotnet

I have one .aspx page that contains multiple user controls. Based on
the form requested, I would like to loop through all of the user
controls on the page and display the one requested by using the ID of
the user control.

The following code is used in my .aspx page containing all the user
controls.

Is there an easier way to accomplish this other than the way I am
currently doing it:

foreach(Control __cntrl in this.Controls)
{
if (__cntrl is MasterPage)
{
foreach (Control cntrl in __cntrl.Controls)
{
if (cntrl is HtmlForm)
{
foreach (Control cnt in cntrl.Controls)
{
if (cnt is ContentPlaceHolder &&
cnt.ID == "MyContentArea")
{
foreach (Control cn in
cnt.Controls)
{
if (cn is UserControl)
{
if (cn.ID == formID)
{
cn.Visible = true;
}
else
{
cn.Visible = false;
}
}
}
}
}
}
}
}
}
 
G

Guest

I can't think of one this early in the morning. If it ain't broke, then don't
fix it.
Peter
 
G

Guest

I think that the code could be simplified a little:
inside your page you could use Page.Form to get a reference to your HTML
form; - this will get rid of the first two loops; do not think that you can
simplify it much further though :-(
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top