Control for marking current page

Topics: Controls
Oct 17, 2007 at 7:09 AM
Here's a control which marks renders an unordered list of the menu and marks the <li> for the current page with a "current" class.
I dunno if this is done already, but if not.. here you go.
Contact, Archive and Search is hardcoded. Check the code out. It's pretty selfexplanatory.

Cheers!
- jonah

************************************************************************************************************************************************************************************************
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BlogEngine.Core;

public partial class themestestcontrols_topMenu : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}

/// <summary>
/// Sends server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"></see> object, which writes the content to be rendered on the client.
/// </summary>
/// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"></see> object that receives the server control content.</param>
protected override void Render(HtmlTextWriter writer)
{
writer.Write(createMenu());
base.Render(writer);
}

/// <summary>
/// Creates the menu.
/// </summary>
/// <returns></returns>
private string createMenu()
{
HtmlGenericControl ul = new HtmlGenericControl("ul");
string sUrl = HttpUtility.UrlEncode(GetPageName(HttpContext.Current.Request.RawUrl.ToLower()));

foreach (BlogEngine.Core.Page page in BlogEngine.Core.Page.Pages)
{
if (page.ShowInList && page.IsPublished)
{
HtmlGenericControl li = new HtmlGenericControl("li");
HtmlAnchor anc = new HtmlAnchor();
anc.HRef = page.RelativeLink.ToString();
anc.InnerHtml = "<span>" + page.Title + "</span>";
anc.Title = page.Description;

string sTab = GetPageName(anc.HRef.ToLower());

if (page.IsFrontPage && (sUrl == "default.aspx"))
{
anc.Attributes.Add("class", "current");
}
else
{
if (string.Compare(sUrl, sTab) == 0)
{
anc.Attributes.Add("class", "current");
}
}

li.Controls.Add(anc);
ul.Controls.Add(li);
}
}

// Add links for contactpage, archive and search
HtmlGenericControl liContact = new HtmlGenericControl("li");
HtmlAnchor contactAnc = new HtmlAnchor();
contactAnc.HRef = Utils.RelativeWebRoot + "contact.aspx";
contactAnc.InnerHtml = "<span>Contact</span>";
contactAnc.Title = "Contact";
if (sUrl == "contact.aspx")
contactAnc.Attributes.Add("class", "current");
liContact.Controls.Add(contactAnc);
ul.Controls.Add(liContact);

HtmlGenericControl liArchive = new HtmlGenericControl("li");
HtmlAnchor archiveAnc = new HtmlAnchor();
archiveAnc.HRef = Utils.RelativeWebRoot + "archive.aspx";
archiveAnc.InnerHtml = "<span>Archive</span>";
archiveAnc.Title = "Archive";
if (sUrl == "archive.aspx")
archiveAnc.Attributes.Add("class", "current");
liArchive.Controls.Add(archiveAnc);
ul.Controls.Add(liArchive);

HtmlGenericControl liSearch = new HtmlGenericControl("li");
HtmlAnchor searchAnc = new HtmlAnchor();
searchAnc.HRef = Utils.RelativeWebRoot + "search.aspx";
searchAnc.InnerHtml = "<span>Search</span>";
searchAnc.Title = "Search";
if (sUrl == "search.aspx")
searchAnc.Attributes.Add("class", "current");
liSearch.Controls.Add(searchAnc);
ul.Controls.Add(liSearch);

System.IO.StringWriter sw = new System.IO.StringWriter();
ul.RenderControl(new HtmlTextWriter(sw));

return sw.ToString();
}

/// <summary>
/// Gets the name of the page.
/// </summary>
/// <param name="requestPath">The request path.</param>
/// <returns></returns>
public static string GetPageName(string requestPath)
{
if (requestPath.IndexOf('?') != -1)
requestPath = requestPath.Substring(0, requestPath.IndexOf('?'));
return requestPath.Remove(0, requestPath.LastIndexOf("/") + 1);
}

}
************************************************************************************************************************************************************************************************