Our Blog

BlogEngine.Net custom control

by BillyTheKid March 11, 2009 18:45

Well, Sly installed BlogEngine.Net and he was pretty impressed with it so I though I would take a stab at creating a custom theme and copying the new skin from our site to see if I could get it to work.  Long story short it was pretty easy and only took me a few hours to do.

Most of the BlogEngine themes had a top nav with home,archive,subscribe,login/logout.  I wanted to put those links into a box in the right column and have our sites nav there.  Not satisfied with just putting  bit of HTML in the master file for the theme, I decided to make my own custom control for it. 

First I created a file App_Code/Controls/BlogMenu.cs

using System;
using System.Web;
using System.Web.UI;
using System.Text;
using BlogEngine.Core;
 
namespace Controls {
 
    public class BlogMenu : Control {
 
    static BlogMenu() {
      BlogEngine.Core.Page.Saved += delegate { _Html = null; };
    }
 
    private static object _SyncRoot = new object(); 
    private static string _Html;
 
    private string Html {
        get {
            lock (_SyncRoot) {
                BuildHtml();
            }
            return _Html;
        }
    }
 
    private void BuildHtml() {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("<div id=\"blogmenu\">");
        sb.AppendFormat("<a href=\"{0}\" rel=\"home\">Blog Home</a><br/>",Utils.AbsoluteWebRoot);
        sb.AppendFormat("<a href=\"{0}archive.aspx\">{1}</a><br/>",Utils.AbsoluteWebRoot,Resources.labels.archive);
        sb.AppendFormat("<a href=\"{0}\" class=\"feed\">{1}</a><br/>",Utils.FeedUrl,Resources.labels.subscribe);
        sb.AppendFormat("<a href=\"{0}{1}\">{2}</a>",Utils.AbsoluteWebRoot,(Page.User.Identity.IsAuthenticated)?"login.aspx?logoff":"login.aspx",(Page.User.Identity.IsAuthenticated)?Resources.labels.logoff:Resources.labels.login);
        sb.AppendLine("</div>");
        _Html = sb.ToString();
    }
 
    public override void RenderControl(HtmlTextWriter writer) {
      writer.Write(Html);
    }
 
  }
 
}

Then in the widgets folder, I created a sub folder called “Blog Menu” and put two files.

first, widget.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="widget.ascx.cs" Inherits="widgets_Blog_Menu_widget" %>
<blog:BlogMenu runat="Server" />

then the second, widget.ascx.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class widgets_Blog_Menu_widget : WidgetBase {
 
    public override string Name {
        get {return "Blog Menu"; }
    }
 
    public override bool IsEditable {
        get { return false; }
    }
 
    public override void LoadWidget() {
        // No action
    }
}

That was it, I logged in as an administrator and could add or remove my Blog Menu widget/control and drag it around with the rest to reorder.

Tags: ,

Coding | English

Comments

05/21/2009 05:01:10 #

Thats pretty cool man! Nice one.

Darren South Africa

Comments are closed