SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to hide a usercontrol that I have added to a custom template based on what page I am on. I have several pages that use the template but I only want one page to display the control the other pages have no need for it. I was thinking about getting the filename in my usercontrol codebehind and then not running any code if it doesn't match my page name. Is this possible to do or do I need to go about it another way?

share|improve this question
up vote 2 down vote accepted

You could certainly create logic that modifies the visibility of a control based on context, such as the page name. But I would avoid having logic that depends on hard-coded "magic" page names - I think it will lead to confusion further down the line.

When you say "template" I assume you mean page layout, and I would suggest that if you have two categories of page that have different layout that you simply create two page layouts for them, one with and one without the control. This is the job of the page layout, with the proviso that you don't want to end up with a proliferation of page layouts. You have to use judgement.

On the other hand, if the display of the control is something like a property of the page you could do it that way - add a field to the page content type (visible in edit mode) that will switch this control on or off. There might be a no-code way of doing that, but I can't think of one at the moment.

One final option to consider - have a web part zone and make the user control a web part. Then you can add it to just the page in question. Very flexible, but you might need to consider performance implications if the page is heavily used.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.