Introduction: In
this article I will expalin how we can Add Meta Tag Code behind in asp.net.
Description:
In the last article I have explained Encrypt and Decrypt Query String in asp.net, Create multiple Language website in Asp.net and Send Password Reset Link in Asp.net.
We need to add Meta tag code behind/Dynamically in
application. Suppose in application we create Content pages from Master page
but we want to add different page title, Title, Description and keyword for
each page.
Add a new webform or create content page from master page.
On .aspx.cs page write the mention
code:
protected void Page_Load(object sender, EventArgs
e)
{
//add meta tag dynamically
Page.Title = "Enter Page Title";
//add meta title
HtmlMeta metatitle = new
HtmlMeta();
metatitle.Name = "Title";
metatitle.Content = "Enter Meta
Title";
Page.Header.Controls.Add(metatitle);
//add meta description
HtmlMeta metadesc = new
HtmlMeta();
metadesc.Name = "description";
metadesc.Content = "Enter
Description";
Page.Header.Controls.Add(metadesc);
//add meta keywords
HtmlMeta metakeyword = new
HtmlMeta();
metakeyword.Name = "keywords";
metakeyword.Content = "Enter
Keywords";
Page.Header.Controls.Add(metakeyword);
}
In VB (.aspx.vb)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
'add meta tag dynamically
Page.Title = "Enter Page Title"
'add meta title
Dim metatitle As New HtmlMeta()
metatitle.Name = "Title"
metatitle.Content = "Enter Meta
Title"
Page.Header.Controls.Add(metatitle)
'add meta description
Dim metadesc As New HtmlMeta()
metadesc.Name = "description"
metadesc.Content = "Enter
Description"
Page.Header.Controls.Add(metadesc)
'add meta keywords
Dim metakeyword As New HtmlMeta()
metakeyword.Name = "keywords"
metakeyword.Content = "Enter
Keywords"
Page.Header.Controls.Add(metakeyword)
End Sub
Run the project. To check Meta Tag right click on page>>View Page Source. See in Head tag Meta Tags are added to page.
No comments:
Post a Comment