Customize View Of PostView In BlogEngine Home Page

When you are using BlogEngine.Net out of the box, you will notice that display of blog posts on home are controlled by PostView.ascx user control in theme of your blog. A lot of time there may be some content in your post view that you only want to show when user is visiting page for that post and you do not want to show it on home page. For example in my blog I display google adsense block on each post. Since google allows only 3 ads on a page, that means on my home page I will have large empty blocks under each post. And in some cases you may want to hide some content that may increase load time of the home page because each post will need to render that part of the content. Bottom line is that since PostView.ascx controls the view of posts on home page as well, you may need some way of controlling that view depending on page where it is being used.

Here is a quick way to fix this without making any change to BlogEngine core. I have included parts of the view that I want to control in ASP.Net PlaceHolder control. And then in Page_Load or RenderControl event of PostView control, I have set the visibility of the panel based on URL. Following code snippet shows what I did on my blog.

<asp:PlaceHolder ID="GoogleAdBlock" runat="server">
.... ad script goes here ....
</asp:PlaceHolder>

<script runat="server">
public override void RenderControl(HtmlTextWriter writer)
{
   GoogleAdBlock.Visible = !Request.Path.ToUpper().Contains("POSTS.ASPX");
   base.RenderControl(writer);
}

</script>

You may be wondering why I am checking for POSTS.ASPX and not Default.aspx. I actually have modified BlogEngine core to control what my home page is going to be. I have removed hard coded Default.aspx value from core itself and now it is controlled by a new entry in settings.

Search

Social

Weather

-0.8 °C / 30.6 °F

weather conditions Mist

Monthly Posts

Blog Tags