ASP.NET WebFormページでtitleタグをコンテンツページの ContentPlaceHolder で設定しても正しく動作しない - ASP.NET

ASP.NET WebFormアプリケーションでマスターページを利用した場合に、 コンテンツページでheadタグ内のtitleタグを ContentPlaceHolderで設定してもうまく動作しない現象と対処法を紹介します。

不具合の起きるコード

マスターページ

<%@ 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がブラウザに返されます。
<!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>

一見正しそうですが、<head>タグ内に<title>タグが2つ含まれており、最初のtitleタグが空になっています。この状態ではタイトルタグが設定されていない状態と変わりません。

対策

コンテンツページの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>

意図した結果になりました。

著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2023-12-27
作成日: 2013-01-07
iPentec all rights reserverd.