ASP.NET WebFormアプリケーションでマスターページを利用した場合に、
コンテンツページでheadタグ内のtitleタグを ContentPlaceHolderで設定してもうまく動作しない現象と対処法を紹介します。
ASP.NET WebFormアプリケーションでマスターページを利用し、コンテンツページでContentPlaceHolderを利用してtitleタグを変更した場合に、
コンテンツページを表示した際にtitleタグが設定されていない状態になります。
<%@ Master Language="C#" AutoEventWireup="true"
CodeBehind="Site.master.cs" Inherits="UserAgentList.Site" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="Container">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</div>
</form>
</body>
</html>
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="SimpleApp.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<title>コンテンツのタイトル</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="TitleFrame">コンテンツです。</div>
</asp:Content>
先のコードのプロジェクトを実行すると以下のHTMLページが表示されます。
一見正しそうですが、<head>タグ内に<title>タグが2つ含まれており、最初のtitleタグが空になっています。
この状態ではタイトルタグが設定されていない状態と判断されていしまう場合があります。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<title>コンテンツのタイトル</title>
</head>
<body>
<form method="post" action="ColorToHsv.aspx" id="form1">
<div class="Container">
<div class="TitleFrame">コンテンツです。</div>
</div>
</form>
</body>
</html>
コンテンツページのContentPlaceHolder内ではなく、Pageディレクティブ内のtitleにタイトルを設定すると正しく動作します。
<%@ Master Language="C#" AutoEventWireup="true"
CodeBehind="Site.master.cs" Inherits="UserAgentList.Site" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="Container">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</div>
</form>
</body>
</html>
<%@ Page Title="コンテンツのタイトル" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="SimpleApp.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="TitleFrame">コンテンツです。</div>
</asp:Content>
プロジェクトを実行すると以下のHTMLがブラウザに返されます。
意図した結果になりました。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
コンテンツのタイトル
</title>
</head>
<body>
<form method="post" action="ColorToHsv.aspx" id="form1">
<div class="Container">
<div class="TitleFrame">コンテンツです。</div>
</div>
</form>
</body>
</html>