Skip to main content

ASP.Net DotNetNuke Page Header Head/Meta Tags

Adding meta, css and script tag to the header proved to be too much trial and error. And searching online results in no clear solution either.

My custom requirement was for DotNetNuke Module to add css and script tag at page level head tag not inside the body tag.

Here is the simple solution:
In my view.ascx.vb file, I have added a prerender block of script:


Private Sub Page_PreRender(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.PreRender


Try
    Dim htmlHeadTags As String
    Dim htmlHeadCtrl As LiteralControl = New LiteralControl

    htmlHeadTags = "<meta name=""viewport"" content=""initial-scale=1.0, user-scalable=no"" />" _
& "<link href=""http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.12/themes/flick/jquery-ui.css"" rel=""stylesheet"" type=""text/css""/>" _
& "<link id=""ServiceCenterCSS"" rel=""stylesheet"" type=""text/css"" href=""" & ResolveUrl("~/DesktopModules/ServiceCenter/ServiceCenter.css") & """ />" _
& "<script src=""http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.12/jquery-ui.js""></script>"

    htmlHeadCtrl.Text = htmlHeadTags

    Dim header As Web.UI.HtmlControls.HtmlHead
    header = TryCast(Me.Page.Header, Web.UI.HtmlControls.HtmlHead)
    If header IsNot Nothing Then
    
    header.Controls.Add(htmlHeadCtrl)
    End If

    Catch exc As Exception 'Module failed to load

        
        ProcessModuleLoadException(Me, exc)
    End Try
End Sub



Note:
  1. I have added script later than style-sheets as its the recommended way for rendering the style-sheets and JavaScript.
  2. Also note that if you need to use the double quotes within the "...", you just use two double quotes to avoid the compilation or error message.

Please add your thoughts if you use similar or better approach to this requirement.

Thanks,
Wasay