Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 30th, 2006, 11:46 AM
Authorized User
 
Join Date: Mar 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default MasterPages ContentPages and Meta tags


I saw a post
http://p2p.wrox.com/topic.asp?TOPIC_ID=34053 and wondered if anyone could help me regarding placing meta tags within content pages.

There's a couple of questions I've placed below within the instructions given.



1. In your Master page, add the following tag to the <head> of the page:
  <meta name="keywords" content="" id="MyMetaTag" runat="server" />
2. In the code behind of the Master page, add the following property:
  Public Property MetaData() As String
    Get
      Return MyMetaTag.Content
    End Get
    Set(ByVal value As String)
      MyMetaTag.Content = value
    End Set
  End Property

Question do I have to import anything, and does this have to be placed in the page_load event?

3. In your Content page get a strongly typed version of your master and set the MetaData property like this:
    Dim myMaster As MainMaster = CType(Me.Master, MainMaster)
    myMaster.MetaData = "Hello World"

Where do I place this code in my content page?


thanks.
Michael.

 
Old May 30th, 2006, 03:33 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Michael,

No, you don't need to add an Imports system.

First, add the meta tag as described in my post. Make sure it's placed inside the <head> section that has a runat="server" attribute as well.

Then add the MetaData as a property to the page. You should end up with something like this:
Code:
Partial Class MainMaster 
  Inherits System.Web.UI.MasterPage

  ' Other stuff, like Page_Load etc can go here

  Public Property MetaData() As String
    Get
      Return MyMetaTag.Content
    End Get
    Set(ByVal value As String)
      MyMetaTag.Content = value
    End Set
  End Property

End Class
This gives your master page a property that you can access from other pages.

Then in your content page, set the MetaData property in any place that makes sense. Most likely, this is inside the Page_Load event, like this:
Code:
Protected Sub Page_Load(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles Me.Load
  Dim myMaster As MainMaster = CType(Me.Master, MainMaster)
  myMaster.MetaData = "Hello World"
End Sub
Instead of in Page_Load, you can use the exact same code in a custom method (or any other event) that you have in your page.

Does this help?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old May 31st, 2006, 12:42 AM
Authorized User
 
Join Date: Mar 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,
When I place the code below in my masterpage 'masterpage.master.vb' (I'm using VWD) I get a blue squigley line underneath MyMetaTag. When I hover over it I get the tooltip - Name 'MyMetaTag' is not declared.
any ideas what's wrong?



here's the code for 'masterpage.master.vb'

Partial Class masterpage
    Inherits System.Web.UI.MasterPage

    ' Other stuff, like Page_Load etc can go here

    Public Property MetaData() As String
        Get
            Return MyMetaTag.Content
        End Get
        Set(ByVal value As String)
            MyMetaTag.Content = value
        End Set
    End Property

End Class


Here's part of the MasterPage 'MasterPage.master'

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>Untitled Page</title>
<meta name="keywords" content="" id="MyMetaTag" runat="server" />
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>


And here's the testmonials codebehind file code

Partial Class testimonials
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, _
           ByVal e As System.EventArgs) Handles Me.Load
        Dim myMaster As MasterPage = CType(Me.Master, MasterPage)
        myMaster.MetaData = "Hello World"
    End Sub


End Class

thanks,
Michael.

 
Old May 31st, 2006, 01:39 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

That's weird, and now it's supposed to be. I successfully implemented the meta stuff with the steps I described.

Can you post the code for the master page and its code behind?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to: The Story Ends by Clan of Xymox (Track 13 from the album: Hidden Faces) What's This?
 
Old May 31st, 2006, 02:36 PM
Authorized User
 
Join Date: Mar 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar thank you for all your help it's been greatly appreciated.

The problem was my master page had the directives

<%@ Master Language="VB" %>

and should have been

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage"

thanks,
Michael.


 
Old June 1st, 2006, 04:13 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome. Glad it's working now...

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to: Half the Man by Hed Planet Earth (Track 7 from the album: Blackout ) What's This?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using meta tags amatytfc ASP.NET 2.0 Basics 1 August 6th, 2007 05:47 PM
meta tags ferlach HTML Code Clinic 5 June 5th, 2007 04:17 PM
META tags and master pages thenoseknows ASP.NET 2.0 Professional 1 October 9th, 2006 01:02 PM
Meta tags Adam H-W HTML Code Clinic 2 November 19th, 2003 11:25 AM
Database Driven Website w/ META tags Dan Jallits PHP How-To 2 June 16th, 2003 11:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.