English 中文(简体)
ASP.NET: Having common Page title in master page with each page adding page-specific title?
原标题:

My master page looks like:

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

Content pages look like:

<asp:Content ID="TitleContent1"
     ContentPlaceHolderID="PageTitlePlaceHolder" runat="Server">
    My Page
</asp:Content>

This works by placing the content page specific title on the page ("My Page" in this example). Now I want to add a global prefix to the title in my master page for the site name. So I want:

<head runat="server">
    <title>
        Example.com:
        <asp:ContentPlaceHolder ID="PageTitlePlaceHolder" runat="server" />
    </title>

However, when I do this content pages are still rendered without "Example.com" in the tile, it s like it s ignored.

Why is this happening and how can I achieve this?

问题回答

Try this in the code behind of the MasterPage:

void MasterPage_PreRender(object sender, EventArgs e)
{
    Page.Title = "Example.com - " + Page.Title;
}

Remove the title tag from the master page and use the code Martin provided. Now in your content pages set the title in the Page tag at the top of the file like so:

<%@ Page ... Title="Contact" %>

The work around I found most acceptable is to use multiple ContentPlaceHolder controls.

<head runat="server">
    <title>
        <asp:ContentPlaceHolder ID="cphMasterTitle" runat="server">Example.com: </asp:ContentPlaceHolder>
        <asp:ContentPlaceHolder ID="cphSubtitle" runat="server" />
    </title>
</head>

Note that any other content inside <title>, including whitespace, is stripped away. Any spacing between the ContentPlaceHolder content needs to be done inside the controls.

I ve also used <asp:Literal runat="server">Example.com: </asp:Literal> when I don t want to expose a placeholder to the content pages.

remove the "runat="server"" but i m not sure,try it

<head>
    <title>
        Example.com:
        <asp:ContentPlaceHolder ID="PageTitlePlaceHolder" runat="server" />
    </title>

Two options. one is remove runat=server from <head> (jefferydu)

Two is using the Page.Title as string. (Martin)

I prefer to use a object I wrote somewhere that add also Title for facebook, description for search engines, etc for any page I used. This object stores the page title in three strings- one before, one is page title, one after.

in the master Page, Put

    <title>websitename.com -<%: Page.Title.ToString() %> </title>

where websitename.com is the domain name of the website.

and then in Each Content page , put a title.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签