Suppose that we have visual web part which shows some items (e.g. news). If there are no items to show we want this web part to not take any place on the page. Unfortunately by default Sharepoint reserves some place for empty web parts even if they have Chrome type set to None. In some cases it will cause layout to not look very well. Using the following trick we may hide web part completely:
One additional thing to mention is that we do it when page is not in edit mode (line 10). Otherwise it won’t be possible to edit or delete web part from the page.