473,394 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Show/Hide Multiple User Controls on Page

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;
}
}
}
}
}
}
}
}
}

May 16 '07 #1
2 3734
I can't think of one this early in the morning. If it ain't broke, then don't
fix it.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"vcuankitdotnet" wrote:
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;
}
}
}
}
}
}
}
}
}

May 16 '07 #2
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 :-(

"vcuankitdotnet" wrote:
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;
}
}
}
}
}
}
}
}
}

May 17 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve | last post by:
Visual Studio 2003 C# Windows: I have a tree view control as my main menu control down the left side of my application. This has 2 Parent Nodes on it (Jobs and Employees). beneath these 2 main...
0
by: suzy | last post by:
hello, i have a site where i want to show and hide certain things depending on whether someone is logged in or not. i have a few questions. what is the best approach for this? at what...
5
by: dje | last post by:
In the OnClick event on a radioButtonList, I run a javascript to show/hide the appropriate div along with a submit button, which displays as expected. The problem is the submit no longer works on...
1
by: Norman | last post by:
Hello All, I have developed the ASP.Net application using .Net 1.1. I want to get one section of the page under hide/show feauture. That means when the user clicks on display I want to display all...
3
by: Nathan Sokalski | last post by:
When I view any page in my application a second time, I recieve the following error: System.Web.TraceContext.AddNewControl(String id, String parentId, String type, Int32 viewStateSize) +313...
8
by: Andrew | last post by:
Hello, friends, We created a drop-down menu bar and a show/hide calendar web user controls mainly using javaScript and html. However, they both have the problem that they are not dispaly as...
4
by: bridgemanusa | last post by:
Hi All: I have a very long page of html that I want to take portions and hide them in divs, then show when a link is clicked. I have the hide show part working when the link is clicked, however...
4
by: =?Utf-8?B?U3JpZGhhcg==?= | last post by:
Hi, Is it possible to Hide/Show controls during a callback? I have a radio button list that does the callback. When it does the callback I need to refresh the grid to reflect the selected value...
2
by: dusk | last post by:
Hi, I have a page with lots of hidden divs which are revealed based on choices made at each 'layer'. So I've used naming convention which represents the order in which each div becomes...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.