views:

1929

answers:

6

My master page has a contentplaceholder in the head tag.

Because I want my page's title to represent the function of the current page and because I want the title to be translated in the user's language I have added a title tag in the page's head's contentplaceholder. All jolly and good except that now there appears a second, empty title tag that off course isn't valid.

Any ideas how to solve this?

A: 

There is an attribute in the @Page directive called Title for setting the title of the page. It is also available accessible as a property on the Page control.

You do not need to explicitly declare a within the tag via the content place holder.

At least it is for basic ASP.NET pages using MasterPages. Not so sure for ASP.NET MVC.

BlackMael
My title needs to be translated and therefor needs the viewdata that contains all the translated strings. The title property cannot access that :(
borisCallens
Try putting your ContentPlaceholder inside the title tags.Although I don't see why you can't work out the translation in the controller, and pass the correct one through to the ViewData.See http://stackoverflow.com/questions/192465/how-to-localize-asp-net-mvc-application for some help on this.
Zhaph - Ben Duguid
No, that's not the problem. My translation works all fine. But I have to insert the translated data in the title tag. This cannot be done in the @Page directive.Furthermore I could indeed add a contentplaceholder dedicated for title. But that only solves the simptoms, not the cause.
borisCallens
+5  A: 

According to the W3C spec:

Every HTML document must have a TITLE element in the HEAD section.

Therefore, the ASP.Net platform is conforming to standards and adding an empty title tag to your page to help you achieve valid markup - it doesn't know you are about to add one through a content placeholder.

Under classic ASP.Net your options are:

  1. Use the @page directive Title to set the content of this tag
  2. Use the Page.Title property from your code behind to set the value programmatically.

If you are using ASP.Net MVC, the default Site.Master file had the following default text:

<title><%= Html.Encode(ViewData["Title"]) %></title>

And the default controller had:

ViewData["Title"] = "Home";

within the action result, again allowing for programmatic access to the page title.

Generally I use the HeadContent content placeholder for adding page specific static scripts and css links.

Zhaph - Ben Duguid
Page specific script and css imports indeed go there. But I figured I could add the title too. Seems not. I'll have to let my controller do it then.
borisCallens
+3  A: 

I ran into the same problem and found a solution that seems to work. It's pretty hacky but at the same time pretty simple. Just add another title tag in the head, put a runat="server" attribute inside it and then set it's visibility to false:

<title visible="false" runat="server"><%-- hack to turn the auto title off --%></title>
Helephant
Indeed hackish, but could work
borisCallens
This solution leaves the pages with no titles at all, trying to fix this problem myself at the moment
JamesStuddart
I got around this by adding another <title> tag another way. In fact the reason why I needed to do this was because our .NET1 code had a way of generating a title tag for each page and the .NET2 title was a duplicate.
Helephant
Worked for me, and was the easiest way to tackle the issue in one of our existing codebases. Thanks!
qstarin
+2  A: 

You could try the solution I posted to this question

John Sheehan
A: 

Clear the runat="server" attribute on masterpages tag

Barbaros Alp
When using Themes this causes the page to error
JamesStuddart
A: 

It would appear the best way to set the title would be to use the title attritube in the markup declaration here:

<%@ Page Title="PAGE NAME HERE" Language="C#" MasterPageFile="~/Controls/MasterPage/MasterPage.master"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

OR Just set it in page_load with

Page.Title = "Your page title"

Hope this somewhat.

JamesStuddart
If I'm not mistaking this is the asp.net classic way.Please note the the asp.net-mvc tag.
borisCallens
I shall hang my head in shame... oops!Web forms not classic ;) lol
JamesStuddart