logo
Welcome Guest! To enable all features please Login. New Registrations are disabled.

Notification

Icon
Error

Options
Go to last post Go to first unread
Joey  
#1 Posted : Tuesday, April 27, 2010 10:34:08 AM(UTC)
Joey

Rank: Administration

Reputation:

Groups: Moderators, Administration, Member
Joined: 7/16/2009(UTC)
Posts: 30
Man
Netherlands
Location: Groesbeek

Thanks: 1 times
This function can be used to overwrite the meta information in the page header.

Code:
Private Sub ReplaceHeaderMetaTags(ByRef Page As System.Web.UI.Page, ByVal Description As String, ByVal Title As String, ByVal Keywords As String)
  ' Replace description, title and keywords from page.
  Dim HeaderInnerText As String = Page.Header.InnerHtml
  Dim HeaderDescriptionRemoved As Boolean = False
  Dim HeaderTitleRemoved As Boolean = False
  Dim HeaderKeywordsRemoved As Boolean = False

  If HeaderInnerText.Contains("<meta name=""description""") And Not String.IsNullOrEmpty(Description) Then
    Dim Temp As String = HeaderInnerText.Substring(HeaderInnerText.IndexOf("<meta name=""description"""))
    Dim Selection As String = Temp.Substring(0, Temp.IndexOf("/>") + 2)
    HeaderInnerText = HeaderInnerText.Replace(Selection, "")
    HeaderDescriptionRemoved = True
  End If
  
  If HeaderInnerText.Contains("<meta name=""keywords""") And Not String.IsNullOrEmpty(Keywords) Then
    Dim Temp As String = HeaderInnerText.Substring(HeaderInnerText.IndexOf("<meta name=""keywords"""))
    Dim Selection As String = Temp.Substring(0, Temp.IndexOf("/>") + 2)
    HeaderInnerText = HeaderInnerText.Replace(Selection, "")
    HeaderKeywordsRemoved = True
  End If

  If HeaderDescriptionRemoved Or HeaderTitleRemoved Or HeaderKeywordsRemoved Then
    Page.Header.InnerHtml = HeaderInnerText
  End If

  If HeaderDescriptionRemoved Or (Not HeaderInnerText.Contains("<meta name=""description""") And Not String.IsNullOrEmpty(Description)) Then
    Dim HtmlMetaKeyDescription As New System.Web.UI.HtmlControls.HtmlMeta()
    HtmlMetaKeyDescription.Name = "description"
    HtmlMetaKeyDescription.Content = Description
    Page.Header.Controls.Add(HtmlMetaKeyDescription)
  End If

  If HeaderKeywordsRemoved Or (Not HeaderInnerText.Contains("<meta name=""keywords""") And Not String.IsNullOrEmpty(Keywords)) Then
    Dim HtmlMetaKeyKeywords As New System.Web.UI.HtmlControls.HtmlMeta()
    HtmlMetaKeyKeywords.Name = "keywords"
    HtmlMetaKeyKeywords.Content = Keywords
    Page.Header.Controls.Add(HtmlMetaKeyKeywords)
  End If

  If Not String.IsNullOrEmpty(Title) Then
    Page.Header.Title = Title
  End If
  
  Dim builder As New System.Text.StringBuilder()
  Dim textWriter As New System.IO.StringWriter(builder)
  Dim writer As New System.Web.UI.HtmlTextWriter(textWriter)

  If Page.Header.HasControls() Then
    For Each c As Control In Page.Header.Controls
      c.RenderControl(writer)
    Next
  End If

  writer.Close()
  textWriter.Close()
  
  Page.Header.InnerHtml = builder.ToString()
End Sub

Wanna join the discussion?! Login to your Sienn Forum forum account. New Registrations are disabled.

Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.