Yesterday I showed you a neat way to hide/show some content based on authentication level and/or the page mode. The cool thing about it was that whenever the control was hidden, the child controls didn’t get instantiated at all preventing you from any kind of performance penalty. Today, when I read the article myself, it struck me: you could extract a piece of the control that I showed you yesterday and make of it a base class for all kind of conditionally visible content like content visible based on a query string parameter or a value of a field of the current page!
Although the code responsible for showing/hiding the content wasn’t that complex it would be a shame if you copy/paste it while developing multiple controls that show/hide content depends on some condition. The best way to manage this would be to create an abstract base class so that every time that you need a conditionally visible control, all that you need to do is to implement the condition!
The code
[ParseChildren(true)]
public abstract class ConditionallyVisibleControl : Control
{
public ITemplate ContentTemplate { get; set; }
public abstract bool ShouldBeVisible { get; }
protected override void CreateChildControls()
{
base.CreateChildControls();
if (ShouldBeVisible && ContentTemplate != null)
{
Control container = new Control();
ContentTemplate.InstantiateIn(container);
Controls.Add(container);
}
}
}
Although it’s just a couple of lines of code, it’s always a good practice to isolate repeating code. Let’s see what would a control that shows content depending on some query string parameter look like.
public class QueryStringTrimmedControl : ConditionallyVisibleControl
{
public string Parameter { get; set; }
public string Value { get; set; }
public override bool ShouldBeVisible
{
get
{
bool shouldBeVisible = Page.Request.QueryString[ParameterName] == Value;
return shouldBeVisible;
}
}
}
That’s really all! Then in your Master Page you would use it like:
<Imtech:QueryStringTrimmedControl Parameter="showInfo" Value="1" runat="server"> <ContentTemplate> ... </ContentTemplate> </Imtech:QueryStringTrimmedControl>
How simple is that?!
You can use the base ConditionallyVisibleControl class and build all kinds of conditionally visible controls on top of it in just a matter of minutes!

















March 16th, 2010 at 3:20 pm
excellent tip …
is there a way to hide specific items in a list?
March 17th, 2010 at 5:49 pm
@stefan demetz: there are many ways you could achieve that. Depending on your scenario you could either use item level security (users cannot see items that they don't have the access to) or use JavaScript to hide the items. If you are however looking for conditionally displaying list items in a list view, there is no simple answer to that.
March 19th, 2010 at 9:28 am
Nice tip,
For people that want to go for the query string approach I have one more quick tip.
When working with publishing sites that have caching don't forget to add "showInfo" to your Vary By Paramater configuration.
Greets
March 20th, 2010 at 12:52 pm
@Steven: Well noticed! Thanks, Steven.
June 1st, 2010 at 7:58 pm
This is a very useful piece of code but I noticed a shortcoming today. I had a web part in this control and my button events weren't firing. I can't think of a good reason why. I solved my particular problem another way but it would be great if we could use this with controls that include buttons.