Search code examples
asp.netmaster-pages

How to make the page title display from the web form when the Master Page already has a title?


When I render default.apsx the page title in the browser tab is taken from the Master Page and I want it to be taken from default.aspx

This is default.aspx

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

    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

            <title>Default.aspx Page Title</title>

    </asp:Content>

This is the Master Page

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Master Page Title</title>
    <asp:ContentPlaceHolder id="head" runat="server">

    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

Solution

  • Depends, you can hard-code the current aspx page.

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "new page title";
    }
    

    Or you can get the title from another source, like the Web.sitemap file.

    If you want to get it from something on the page, the title has to be in a control with runat="server", so you can get the value from code-behind.