Blog

Blogging on programming and life in general.

A Hidden User Control Is A Running User Control

Posted in: ASP.NET

I've been a .NET Developer for around 6 years and it still amazes me how I can overlook something that I never really questioned. For example, when a user control is hidden, I always assumed that all the code it contained would never run since until it was made visible.

However, after being told by one of my work colleagues that in fact a hidden user control will always run, it will just simply is hidden by the client. After searching the web for a definitive answer, I found a StackOverflow post that fully backed up his theory: http://stackoverflow.com/questions/12143693/hiding-user-controls-using-code-behind-does-internal-code-still-run-ie-am-i.

As the StackOverflow post suggests, the most performance efficient way to show/hide a user control is by dynamically loading it in when required.

if (jobs.Count > 0)
{
     MyPlaceholder.Controls.Add(Page.LoadControl("~/Controls/Listings/JobsList.ascx"));
}

As you can see in my example above, I'm loading my user control to a place holder in the page.

blog comments powered by Disqus
;