This project is read-only.

can i have two separate postview.ascx controls?

Topics: Business Logic Layer, Themes
Jan 14, 2011 at 7:37 AM

So the theme i am trying to apply has different styles based on if you are looking at the home page showing the lists of blog posts, versus looking at an individual blog post.

Is there a way for me to either have 2 different postview user controls and apply them based if the user is looking at an individual post or a list of posts? or failing that, can i just set up two different layouts in the user control and at runtime decide which one to display based on what page the user is on?

Jan 14, 2011 at 8:19 AM

Yes, you can do this.  It's easier to use the same PostView.ascx file, and can be done without making a mess.

Here's an example.  I excluded most of the content that you would normally put in there (for brevity).  This example shows how an H1 tag is used when the post is viewed by itself, and an H2 tag when it is viewed with all the other posts.  You don't actually have to do this .. but just showing an example of how it can differ.  The key here is the "Location" property.  The possible values are SinglePost, PostList, SinglePage, Feed, Email, Other.

<% if (Location == ServingLocation.SinglePost) { %>

    single post ...
    <h1><%= Server.HtmlEncode(Post.Title) %></h1>
    rest of code goes here

<% } else { %>

    non-single post ...
    <h2><%= Server.HtmlEncode(Post.Title) %></h2>
    rest of code goes here

<% } %>
Jan 14, 2011 at 6:01 PM

ahh, that's perfect. Thank you!

Jan 16, 2011 at 11:04 AM
Edited Jan 16, 2011 at 11:14 AM

Just tried using this and unfortunately i'm getting an error:

The name 'ServingLocation' does not exist in the current context

I took a look at the postview.ascx for the standard theme and the only thing i noticed different from mine was:


In mind i had a codefile so it was inheriting from that. I went and changed mine to do away with the codefie and had it inherit from the postviewbase as well - didn't help though. I still get that error. Visual studio though doesn't indicate this line as being an error:

<% if (Location == ServingLocation.PostList) { %>

As a test, i tried editing the standard postview.ascx file and put this at the top of the file:

<% if (Location == ServingLocation.PostList) { %>
<% } else { %>
<% } %>


This throws the same error i was getting on my page. Also tried copying and pasting your sample code just to make sure i wasn't making some stupid typo, but that had no effect.

Any ideas what might be wrong?


Jan 16, 2011 at 8:35 PM

Two things you can do (one or the other).

At the top of PostView.ascx, add

<%@ Import Namespace="BlogEngine.Core" %>

... or, fully qualify ServingLocation.PostList as BlogEngine.Core.ServingLocation.PostList.

Jan 16, 2011 at 9:57 PM

That fixed it, thanks.