Usman ur Rehman Ahmed's blog

Change Dynamic Title Tag Position in DNN

Today a task was assigned to me that on a DNN created page, title tag under the head section doesn’t appear as the first element after Head section. From the

SEO perspective the requirement was to render tags in following fashion,

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>title goes here</title>
    <meta name="Description" content="description goes here" />
    <meta name="Keywords" content="keywords go here" />…
</head>

That is, title should appear first before the description tag.However, once you acquire a reference to DNN base page in any module and set the title using,

    Dim CDefault As DotNetNuke.Framework.CDefault = CType(Me.Page, DotNetNuke.Framework.CDefault)
    CDefault.Title = "My Title Goes Here"

The title is rendered even after the style sheet elements as shown here,

<head id="Head">

    <meta id="MetaDescription" content="Description Contents" name="DESCRIPTION"/>
    <meta id="MetaKeywords" content="Meta Keywords Here" name="KEYWORDS"/>
    <meta id="MetaCopyright" content="Copyright Here" name="COPYRIGHT"/>
    <meta id="MetaAuthor" content="Site Info Here" name="AUTHOR"/>
    <meta content="DOCUMENT" name="RESOURCE-TYPE"/><meta content="GLOBAL" name="DISTRIBUTION"/>
    <meta content="1 DAYS" name="REVISIT-AFTER"/>
    <meta content="GENERAL" name="RATING"/>
    <meta content="RevealTrans(Duration=0,Transition=1)" http-equiv="PAGE-ENTER"/>

    <style id="StylePlaceholder" type="text/css"></style>

    <link ... type="text/css" rel="stylesheet"></link>

    <script src="..." type="text/jcss"></script>

    <title>My Title Goes Here</title>
</head>
   
Looking into the Default.aspx page (found under DNN installed directory) shipped with DNN, I found the structure of Head section similar to this,

<%@ Page Language="vb" AutoEventWireup="false" Explicit="True" Inherits="DotNetNuke.Framework.DefaultPage" CodeFile="Default.aspx.vb" %>
<%@ Register TagPrefix="dnn" Namespace="DotNetNuke.Common.Controls" Assembly="DotNetNuke" %>
<%@ Register TagPrefix="dnnui" Namespace="DotNetNuke.Web.UI.WebControls" Assembly="DotNetNuke.Web" %>

     <asp:literal id="skinDocType" runat="server"></asp:literal>   <!-- 1st server side control -->
    <html<%= HtmlAttributeList %>>
    <head id="Head" runat="server">   <!-- 2nd server side control -->
        <meta id="MetaRefresh" runat="Server" name="Refresh" />
        <meta id="MetaDescription" runat="Server" name="DESCRIPTION" />
        <meta id="MetaKeywords" runat="Server" name="KEYWORDS" />
        <meta id="MetaCopyright" runat="Server" name="COPYRIGHT" />
        <meta id="MetaGenerator" runat="Server" name="GENERATOR" />
        <meta id="MetaAuthor" runat="Server" name="AUTHOR" />
        <meta name="RESOURCE-TYPE" content="DOCUMENT">
        <meta name="DISTRIBUTION" content="GLOBAL">
        <meta name="ROBOTS" content="INDEX, FOLLOW">
        <meta name="REVISIT-AFTER" content="1 DAYS">
        <meta name="RATING" content="GENERAL">
        <meta http-equiv="PAGE-ENTER" content="RevealTrans(Duration=0,Transition=1)">
        <style type="text/css" id="StylePlaceholder" runat="server"></style>

        <asp:placeholder id="CSS" runat="server"></asp:placeholder>

    </head>

..
</html>

Head control is found as the second server side control in CDefault controls hierarchy (highlighted above). Thus if you want to chagne the title position in the head section, you need to add a custom literal control at the first position in the head section just as shown below,

''Get reference to Base Page

Dim CDefault As DotNetNuke.Framework.CDefault = CType(Me.Page, DotNetNuke.Framework.CDefault)

''Create a new Literal Control to contain Title
Dim ltrTitle As New LiteralControl

ltrTitle.Text = "<title>My Title Goes Here</title>"

''Add newly defined literal control on the base page as first element
CDefault.Controls(2).Controls.AddAt(0, ltrTitle)

Hope that helps.

11
To Posterous, Love Metalab