Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding title in contentplaceholder adds second empty title tag

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?

like image 358
Boris Callens Avatar asked Dec 16 '08 09:12

Boris Callens


People also ask

How to add title in aspx page?

By placing the value directly within the <title> element. Using the Title attribute in the <%@ Page %> directive. Programmatically setting the page's Title property using code like Page. Title="title" or Page.

How can add ContentPlaceHolder in master page in asp net?

Solution 2 A Content control is associated with a ContentPlaceHolder using its ContentPlaceHolderID property. Set the ContentPlaceHolderID property to the value of the ID property of the related ContentPlaceHolder control in a master page. More than one ContentPlaceHolder can be declared in a master page.


3 Answers

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>
like image 39
Helephant Avatar answered Oct 22 '22 15:10

Helephant


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.

like image 112
Zhaph - Ben Duguid Avatar answered Oct 22 '22 15:10

Zhaph - Ben Duguid


You could try the solution I posted to this question

like image 2
John Sheehan Avatar answered Oct 22 '22 17:10

John Sheehan