brainbolt
brainbolt

Reputation: 616

Change MasterPage portion of page title programmatically

I would like to overwrite the MasterPage title. NOTE: I'm not talking about setting a portion of the title from the content page.

I have this in my MasterPage:

<head id="Head1" runat="server">
    <title>My Site | <% = Page.Title %></title>
</head>

Which, in combination with this on my content pages...

<%@ Page Title="Content page title" ... %>

delivers the following to the user:

<title>My Site | Content page title</title>, which is great 99.9% of the time. However, I would like to be able to change the entire title delivered to the client so that it does not include the portion specified in the head of the MasterPage, IE: <title>Special content page</title>

Is it possible to change the entire page title from the content page programmatically? I do not want to change the configuration of the MasterPage or use a different MasterPage.

EDIT: I also need to be able to implement this without changing existing content pages. Sorry I didn't state this explicitly. I assumed it would be understood

Upvotes: 3

Views: 6847

Answers (4)

davidWazy
davidWazy

Reputation: 61

In Master Page

<title>
    <asp:ContentPlaceHolder ID="cphMasterTitle"
     runat="server"/>
</title>

In Child Page

<asp:Content ID="contentChildTitle" ContentPlaceHolderID="cphMasterTitle"
    runat="server">
    <asp:literal ..... etc ... />
</asp:Content>

or without use of content/placeholder

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Me.Title = "Child Title"
End Sub

Upvotes: 0

tgriffin
tgriffin

Reputation: 525

Change your master page markup to

<head id="Head1" runat="server">
    <title><asp:Literal ID="MasterPageTitle" runat="server" /></title>
</head>

In your masterpage.cs

public string ExplicitTitle { get; set; }

protected void Page_PreRender(object sender, EventArgs e)
{
    if (string.IsNullOrWhiteSpace(this.ExplicitTitle))
    {
        this.MasterPageTitle.Text = "My Site | " + Page.Title;
    }
    else
    {
        this.MasterPageTitle.Text = this.ExplicitTitle;
    }
}

In your "Special content page" add the this to Page_PreRender

((MySiteMaster)this.Master).ExplicitTitle = "Special content title";

Upvotes: 2

djtubig-malicex
djtubig-malicex

Reputation: 1226

For VS2012, in your child pages if using WebForms, you change absolutely nothing in your Master Page. Everything can now be done through your child pages:

The following statement goes at the very top of your child page code:

`<%@ Page Title="Insert Your Title here" Language="C#" MasterPageFile="~/MyMasterPage.Master" AutoEventWireup="true" CodeBehind="SomeChildPage.aspx.cs" Inherits="SomeProject.WebForm1" %>`

Upvotes: 0

Dillie-O
Dillie-O

Reputation: 29725

There are two approaches depending on which form of ASP.Net you're using.

If you're using MVC, then simple set a property called "PageTitle" in your ViewBag and then reference it in your Master Page:

In HomeController.cs

@ViewBag.PageTitle = 'Overridden Title'

In your Master Page:

<head id="Head1" runat="server">
    <title>My Site | <% = @ViewBag.PageTitle %></title>
</head>

If you're using a WebForms based application, you can do this by first exposing the html title through "hybrid server control"

In your master page, change things to:

<head id="Head1" runat="server">
    <title id="MasterTitle" runat="server"></title>
</head>

Then in your child pages, you add a type reference to your master page up at the top:

<%@ MasterType virtualpath="~/Templates/YourMasterPage.master" %> 

In your child page code behind, you now have access through.

Master.MasterTitle.Text = "Overridden Title";

Upvotes: 1

Related Questions