Set meta tags with asp.net
Friday 02 October 2009
If you would like to set the meta tags of your page without using repeaters, then here's how to do it:
1) Insert the below meta tags into your page (notice the id and runat attribute):
- <meta name="description" content="description" id="MetaDescription" runat="server" />
- <meta name="keywords" content="keys" id="MetaKeywords" runat="server" />
If you're using a master page, set a ContentPlaceHolder, just below, within the master page:
- <asp:contentplaceholder ID="ContentMeta" runat="server" />
So if you are using a master page, this is how your meta tags should look:
- <asp:content ID="Content1" ContentPlaceHolderID="ContentMeta" Runat="Server">
- <meta name="description" content="description" id="MetaDescription" runat="server" />
- <meta name="keywords" content="keys" id="MetaKeywords" runat="server" />
- </asp:content>
2) Now in the Page_Load sub of your .vb page, enter the below code:
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- MetaKeywords.Attributes("content") = "keyword1, keyword2)"
- MetaDescription.Attributes("content") = "description, blah blah blah"
- End Sub
If you're populating these meta tags from a database, something like the below will do the job for you:
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- If Not Page.IsPostBack Then
- BindData()
- End If
- End Sub
-
- Private Sub BindData()
- Dim MyConnection As SqlConnection
- MyConnection = New SqlConnection()
- MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
-
- Dim sql As String = "SELECT keywords, description FROM TABLE WHERE ID = 1"
- Dim objDR As SqlDataReader
- Dim CmdR As New SqlCommand(sql, MyConnection)
- MyConnection.Open()
- objDR = CmdR.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
- While objDR.Read()
- MetaKeywords.Attributes("content") = objDR("keywords").Trim()
- MetaDescription.Attributes("content") = objDR("description").Trim()
- End While
- MyConnection.Close()
- End Sub
|
(0)
|
|
|