CodeVerge.Net Beta
Login Idy
Register Password
  Forgot?
Explore    Item Entry    Members   
NEWSGROUP
.NET
Algorithms-Data Structures
Asp.Net
C Plus Plus
CSharp
Database
HTML
Javascript
Linq
Other
Regular Expressions
VB.Net
XML





Zone: > NEWSGROUP > Asp.Net Forum > general_asp.net.master_pages_themes_and_navigation_controls Tags:
Item Type: NewsGroup Date Entered: 4/19/2007 10:31:45 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 9 Views: 13 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
10 Items, 1 Pages 1 |< << Go >> >|
dotnetnoob
Asp.Net User
adding META data when using master page?4/19/2007 10:31:45 PM

0/0

Hi All: I'm struggling with how to programmatically add unique META tags to different pages all using the same master page.

 I tried to add the tags to the Page_Load on the child page, but they're not being picked up when I view source in the browser. My master page has runat server in the head tag.

Is this because the master content (where the <head> tags are) is loaded before the page load is executed? If so, what's the correct way to insert the header data into each page?

I'm using:

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

Dim metadesc As New HtmlMeta()

Dim metakeys As New HtmlMeta()

Dim head As HtmlHead = Page.Header

 

 

metadesc.Name =

"description"

metadesc.Content =

"My Lovely website descriptiong"

head.Controls.Add(metadesc)

 

metakeys.Name =

"keywords"

metakeys.Content =

"my cool keywords"

head.Controls.Add(metakeys)

 

 

End Sub

 Thanks for any hints!

 

Edited by longhorn2005

MorningZ
Asp.Net User
Re: adding META data when using master page?4/20/2007 12:45:13 AM

0/0

There's a link in this post (i'm using this base class way and totally dig it)

http://forums.asp.net/thread/1626804.aspx

Plus the C# way of adding meta deta from code behind, if you need it in VB, it'd be like this:

Dim meta As HtmlMeta = New HtmlMeta
meta.Name = "keywords"
meta.Content = "Your keywords here"
Me.Header.Controls.Add(meta)

meta = New HtmlMeta
meta.Name = "robots"
meta.Content = "noindex"
Me.Header.Controls.Add(meta)


"If you make it idiot proof, they'll build a better idiot"
dotnetnoob
Asp.Net User
Re: adding META data when using master page?4/20/2007 10:30:07 AM

0/0

Thanks, I had tried that code (I found several iterations of this method online) but it didn't insert the Meta tags. I'll have to go back and try again I guess.

I then remembered that I had seen this method and had forgotten all about it...but this seems to work:

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim metaDescLiteral As New Literal

metaDescLiteral.Text = "<meta NAME=""DESCRIPTION"" CONTENT=""My Lovely website description"">" & vbNewLine

Me.Header.Controls.Add(metaDescLiteral)

Dim metaKeyLiteral As New Literal

metaKeyLiteral.Text = "<meta NAME=""KEYWORDS"" CONTENT=""My Lovely Keywords"">" & vbNewLine

Me.Header.Controls.Add(metaKeyLiteral)

End Sub

I'm not sure why this method works and the other doesn't :(

dotnetnoob
Asp.Net User
Re: adding META data when using master page?4/20/2007 10:40:26 AM

0/0

Since I'm dotNetNoob I went back and tried your VB example code one more time on my child page:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim meta As HtmlMeta = New HtmlMeta
meta.Name =
"keywords"
meta.Content = "My Lovely Description"
Me.Header.Controls.Add(meta)

meta.Name =

"DESCRIPTION"
meta.Content = "My Lovely Keywords"
Me.Header.Controls.Add(meta)

meta =

New HtmlMeta
meta.Name =
"robots"
meta.Content = "noindex"
Me.Header.Controls.Add(meta)

End Sub

But still no love...am I missing something essential that needs to be done on the master page?

will9key
Asp.Net User
Re: adding META data when using master page?4/20/2007 4:01:30 PM

0/0

Add another content control and place it between the head tags of the master page. Then you can add meta tags, dynamic titles, javaScript etc in your content page.
dotnetnoob
Asp.Net User
Re: adding META data when using master page?4/20/2007 4:24:43 PM

0/0

Is that any better than declaring the literals on the child pages?
MorningZ
Asp.Net User
Re: adding META data when using master page?4/20/2007 4:43:15 PM

0/0

dotnetnoob:

Since I'm dotNetNoob I went back and tried your VB example code one more time on my child page:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim meta As HtmlMeta = New HtmlMeta
meta.Name =
"keywords"
meta.Content = "My Lovely Description"
Me.Header.Controls.Add(meta)

meta.Name =

"DESCRIPTION"
meta.Content = "My Lovely Keywords"
Me.Header.Controls.Add(meta)

meta =

New HtmlMeta
meta.Name =
"robots"
meta.Content = "noindex"
Me.Header.Controls.Add(meta)

End Sub

But still no love...am I missing something essential that needs to be done on the master page?

Well to start....   what does "still no love" mean?   do you get errors?  does it do anything?  have you tried using the debugger or tracing to see what's happening?

 Secondly, when you go to set "NAME", you do not specify the item "meta" as a "New" object so it is going to cause conflicts with the "keywords" declaration

 

dotnetnoob:
Is that any better than declaring the literals on the child pages?

Absolutely, as you can add them right on the aspx page, which will give you intellisense, although the way to do this in the code is really simple and straight forward, no idea why it isn't working for you


"If you make it idiot proof, they'll build a better idiot"
dotnetnoob
Asp.Net User
Re: adding META data when using master page?4/20/2007 5:45:24 PM

0/0

"Still no love" means "It still does not work". I was viewing the source but not seeing anything other than the title in the header...

But, being a noob, I copied the code back into the page one more time, viewed it in the browser, and now it seems to be working <sigh>...It's entirely possible that when I viewed the source, I didn't even notice the inserted tags because they were appearing all in a single line and I don't have wordwrap on in my default viewer...oh well...thanks for your persistence, MorningZ!

Zhao Ji Ma - MS
Asp.Net User
Re: adding META data when using master page?4/23/2007 7:59:17 AM

0/0

Hi dotnetnoob,

Here is one example:

 In Master Page, the <head> tag must support runat="server" like:

...

<head runat="server">
<title>Master Page</title>
</head>

...

In each of the Content page, you can add Meta tag as following:

HtmlHead head = (System.Web.UI.HtmlControls.HtmlHead)Header;

HtmlMeta meta = new HtmlMeta();

//Specify meta attributes
meta.Attributes.Add("name", "keywords");
meta.Attributes.Add("content", "keyword1, keyword2, keyword3");

// Add the meta object to the htmlhead's control collection
head.Controls.Add(meta);

Good Luck.

 


Zhao Ji Ma
Sincerely,
Microsoft Online Community Support

?Please remember to click ?Mark as Answer? on the post that helps you, and to click ?Unmark as Answer? if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ?
jaydubal
Asp.Net User
Re: adding META data when using master page?1/1/2008 1:22:40 PM

0/0

 Hi, You can use a more simpler and flexible solution:

Step 1: In your master page define this:

 
 ....

<head>
    <asp:ContentPlaceHolder ID="ContentSEO" runat="server" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="content-language" content="en" />

    ...

    ....
  
  

Step 2: Now within the child pages, add this:

 

 

<%@ Page Language="VB" MasterPageFile="~/masters/Default.master" AutoEventWireup="false"
    CodeFile="Default.aspx.vb" Inherits="home_Default" Title="Untitled Page" %>
<asp:Content ID="ctlSEO" ContentPlaceHolderID="ContentSEO" runat="server">
    <title>Your title</title>
    <meta name="Keywords" content="your,keywords" />
    <meta name="Description" content="Your Keywords" />
</asp:Content>
 
 

Cool?

Thanks

free asp.net ebooks

10 Items, 1 Pages 1 |< << Go >> >|



Search This Site:


Meet Our Sponsors:



Other Resources:

master page not displaying properly - newsgroup.asp-net-forum ... When your adding the new page, are you making sure to keep the <div id=""> all ... Displaying Master-Detail Data : This chapter describes how to create various ...
CodeProject: Using Meta Tags with Master Pages in ASP.NET. Free source ... Using Meta Tags with Master Pages in ASP.NET. By Jim Azar ... info on dynamically adding meta data when using master pages, and I have ...
Adding unique meta tags to each page - ASP.NET Forums ... to do to the master page to allow the use of meta tags on each page on my site? Thanks. ... WindowsClient: Metadata for ADO.NET Data Services ...
Adding Code-Behind Files to Master Pages and Page Layouts in SharePoint ... Page meta:progid="SharePoint.WebPartPages.Document" Language ... One Business Data Catalog List from Another by Using Associations in SharePoint Server 2007 ...
adding meta tags - ASP.NET Forums ... tell me the proper way of adding meta tags to a page that is using a master page. ... cooler way to do it is to expose the meta data as properties of the Master ...
CodeProject: Adding attributes to the <body> tag when using Master ... Adding attributes to the <body> tag when using Master Pages. By Chuck Bevitt ... You can access the meta tags and the html tag, for example. ...


 
All Times Are GMT