Home > .NET, asp.net, VB > ASP.NET Move Link Elements from the Body Element to Head

ASP.NET Move Link Elements from the Body Element to Head


ASP.NET: Move Link Elements from the Body Element to Head

Today at a client site, we use a custom asp.net template and some 3rd party custom controls. to build the asp.net pages The custom template is a standard template by the company and we have to use it. Long story short, the template doesn’t initialize the Head property on the page. Hence when 3rd part custom controls finds that page.head ==null, it registers the webresources, as link elements, into the body of the page. Hence, the page fails xhtml strict validation check.

To fix that behaviour I wrote the following code:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim sbPageHtml As New StringBuilder
        'Get the html to be rendered
MyBase.Render(New HtmlTextWriter(New StringWriter(sbPageHtml)))
Dim strPagHtml = sbPageHtml.ToString()
'find the occurences of
Dim matches() As String = (From m As Match In Regex.Matches(strPagHtml, "*") _
Select m.Value).ToArray()
        'matches exist
        If matches.Length > 0 Then
'remove occurances from html
Dim toRenderPageHtml As String = Regex.Replace(strPagHtml, "*", "")
           'Write found link tags into the head tag
toRenderPageHtml = toRenderPageHtml.Replace("", String.Format("{0}", String.Join("", matches)))
            writer.Write(toRenderPageHtml)

        Else 'no matches found, no modifications required
writer.Write(strPagHtml)
        End If
    End Sub

If you can think of other solutions, feel free to post them in the comment section.

Advertisements
Categories: .NET, asp.net, VB Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: