Set meta tags with asp.net




Set meta tags with asp.net
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):
  1. <meta name="description" content="description" id="MetaDescription" runat="server" />    
  2. <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:
  1. <asp:contentplaceholder ID="ContentMeta" runat="server" />  
So if you are using a master page, this is how your meta tags should look:
  1. <asp:content ID="Content1" ContentPlaceHolderID="ContentMeta" Runat="Server">  
  2. <meta name="description" content="description" id="MetaDescription" runat="server" />    
  3. <meta name="keywords" content="keys" id="MetaKeywords" runat="server" />  
  4. </asp:content>  
2) Now in the Page_Load sub of your .vb page, enter the below code:
  1. Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load   
  2.         MetaKeywords.Attributes("content") = "keyword1, keyword2)"  
  3.         MetaDescription.Attributes("content") = "description, blah blah blah"  
  4. End Sub  
If you're populating these meta tags from a database, something like the below will do the job for you: 
  1. Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load   
  2.         If Not Page.IsPostBack Then  
  3.             BindData()   
  4.         End If  
  5. End Sub  
  6.   
  7. Private Sub BindData()   
  8.         Dim MyConnection As SqlConnection   
  9.         MyConnection = New SqlConnection()   
  10.         MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString   
  11.   
  12.         Dim sql As String = "SELECT keywords, description FROM TABLE WHERE ID = 1"  
  13.         Dim objDR As SqlDataReader   
  14.         Dim CmdR As New SqlCommand(sql, MyConnection)   
  15.         MyConnection.Open()   
  16.         objDR = CmdR.ExecuteReader(System.Data.CommandBehavior.CloseConnection)   
  17.         While objDR.Read()   
  18.             MetaKeywords.Attributes("content") = objDR("keywords").Trim()   
  19.             MetaDescription.Attributes("content") = objDR("description").Trim()   
  20.         End While  
  21.         MyConnection.Close()   
  22. End Sub  


No one has said that they like article yet, be the first and click here (0)
Send a link to this page via email Send a link to this page