Show/Hide Multiple User Controls on Page

  • Thread starter Thread starter vcuankitdotnet
  • Start date Start date
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;
}
}
}
}
}
}
}
}
}
 
I can't think of one this early in the morning. If it ain't broke, then don't
fix it.
Peter
 
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 :-(
 
Back
Top