Question

I have a site with a few pages, and I'd like the titles of the pages to be:

Foo - 1st Page
Foo - 2nd Page
Foo - 3rd Page

I've created a Master Page with the following code:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Foo.master.cs" Inherits="Foo" %>

<!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>Foo - <asp:ContentPlaceHolder ID="SubTitle" runat="server"></asp:ContentPlaceHolder></title>
</head>
<body>
    <asp:ContentPlaceHolder ID="MainContent" runat="server">
    </asp:ContentPlaceHolder>
</body>
</html>

And then each page looks like this:

<%@ Page Language="C#" MasterPageFile="~/Foo.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="SubTitle" runat="server">1st Page</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <a href="Page2.aspx">Page 2</a>
</asp:Content>

When I load the page in the browser I expect the title to be Foo - 1st Page, but it's just 1st Page.

The html source is :

<!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>
  1st Page
</title></head>
<body>

<a href="Page2.aspx">Page 2</a>

</body>
</html>

What am I doing wrong?

Was it helpful?

Solution

You do not want to remove runat="server" from head; it'll create other problems.

Use the following method which is a default method in ASP.Net Web Forms Application in Visual Studio 2012.

<!-- Master Page -->
<head runat="server">
    <title>Foo - <%: Page.Title %></title>
</head>

<!-- Content Page/xxx.aspx -->
<%@ Page Title="Home Page" ...>

OTHER TIPS

Turns out this question had already been asked: ASP.NET: Having common Page title in master page with each page adding page-specific title?

After reading the answers there I found that removing runat="server" from the head element in the Master Page did the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top