views:

384

answers:

2

I'm attempting to completely hide a custom web part I'm writing under specific circumstances. What I'm not finding a lot of help on is actually hiding the complete web part. Using:

this.Hide = true;

will hide the content of the web part, but it appears to leave the Chrome behind. I can't change the Chrome display, so that's not an option right now. Is there a more complete way to hide the complete web part using C#?

+2  A: 

The only way to ensure nothing is rendered to the page is to close the web part. From MSDN:

A WebPart control that is hidden remains in its zone, is actually rendered on its page, and participates in page life cycle phases such as the PreRender phase, even though it is not visible in a browser. This is different from WebPart controls that are closed, because closed controls are not even rendered on a page...

Use the SPLimitedWebPartManager and its CloseWebPart method to do this. (Be careful about disposing of SPLimitedWebPartManager as well as its Web property correctly.)

Alternatively, use a CSS or jQuery approach. The web part will still be rendered but you will be able to hide it as you originally wanted.

Alex Angas
+2  A: 

I'm not worried about the web part being rendered or not, just having it not visually display. What I ended up doing is changing the chrome through code if my specific situation arises.

this.ChromeType = PartChromeType.None;
this.Hidden = true;
Chris Stewart
That should do it.
Alex Angas