Use Multiple Masters in 1 blog theme

Topics: ASP.NET 2.0, Themes
Aug 14, 2008 at 10:14 PM
I am trying to see if it is possible to select a different master page, if you are on a particular page. 

Is this possible, if so how?

The code I think will handle it is:
 I think somewhere in here I need to put a if/else. I am not a programmer so I don't fully understand every part of this.

Thanks in advance.

protected override void OnPreInit(EventArgs e)
        {
           
          if (Request.QueryString["theme"] != null)
                    _Theme = Request.QueryString["theme"];

                MasterPageFile = Utils.RelativeWebRoot + "themes/" + _Theme + "/site.master";
         
            base.OnPreInit(e);

            if (!Page.IsPostBack && !string.IsNullOrEmpty(Request.QueryString["deletepost"]))
            {
                if (Page.User.Identity.IsAuthenticated)
                {
                    Post post = BlogEngine.Core.Post.GetPost(new Guid(Request.QueryString["deletepost"]));
                    if (Page.User.IsInRole(BlogSettings.Instance.AdministratorRole) || Page.User.Identity.Name == post.Author)
                    {
                        post.Delete();
                        post.Save();
                        Response.Redirect(Utils.RelativeWebRoot);
                    }
                }
            }
        }

Aug 15, 2008 at 4:33 AM
mbulfair,

I planned on blogging about this and still plan to, but this should be able to give you what you need.  You're close. 

1) Add a masterfilename property in BlogBasePage.cs

        // DBVT Modified  - Added Master Page property

        private string dbvtMasterPage = "site.master";

        public string DBVTMasterPage
        {
            get { return dbvtMasterPage; }
            set { dbvtMasterPage = value; }
        }

        // DBVT End


2) Change  the line you referred to in BlogBasePage.cs

MasterPageFile =

Utils.RelativeWebRoot + "themes/" + _Theme + "/" + this.DBVTMasterPage;

3) In the page codeBehind.cs file for which you want to use a different theme, say, Search.aspx.cs, add the following.

    protected override void OnPreInit(EventArgs e)
    {
        this.DBVTMasterPage = "search.master";
        base.OnPreInit(e);
    }

That should cover it. 

Regards,
Dave

 

Aug 15, 2008 at 1:44 PM
Where do you put # 1 exactly? If I place it inside the  onPreInt, the rest of the code breaks.. If I place it outside line # 2 becomes invalid.

Also how would you handle virtual pages. I created a page in the admin. Which I think references page.aspx but I only need this second master to be on this 1 page.

Thanks though, 1 step closer.

 
Aug 15, 2008 at 3:46 PM
Perhaps this blog post will help.

http://dbvt.com/blog/post/Supporting-Multiple-Master-Pages-in-BlogEngineNET.aspx

Also read Al's comment on modifying the site.Master codebehind to dynamically change the page display.  That might be easier depending on what you want to do.

Regards,
Dave

Oct 24, 2008 at 6:27 PM
Edited Oct 24, 2008 at 6:27 PM
Hi,
Deleting a Post does not appear to be working in IE 6.
It works in Firefox 2 well. When I click on the Delete link of any Post (in IE 6) the page just stays there.
When I go to some other page and come back then I can see that it is delete.

It looks like the post is getting deleted, but the page is not getting refreshed for some reason. And this is an issue only with IE 6.

                if (Page.User.Identity.IsAuthenticated)
                {
                    Post post = BlogEngine.Core.Post.GetPost(new Guid(Request.QueryString["deletepost"]));
                    if (Page.User.IsInRole(BlogSettings.Instance.AdministratorRole) || Page.User.Identity.Name == post.Author)
                    {
                        post.Delete();
                        post.Save();
                        Response.Redirect(Utils.RelativeWebRoot);
                    }
                }