2013-03-21 92 views
2

我想覆蓋MasterPage標題。注意:我不是在設置內容頁面的部分標題。以編程方式更改頁面標題的MasterPage部分

我有這個在我的母版:

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

其中,與本我的內容頁面的組合...

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

提供了以下用戶:

<title>My Site | Content page title</title> ,這是99.9%的時間。但是,我希望能夠更改交付給客戶端的整個標題,使其不包含MasterPage頭部指定的部分,即IE:<title>Special content page</title>

是否可以將整個頁面標題從內容頁面編程?我不想更改MasterPage的配置或使用不同的MasterPage。

編輯:我也需要能夠實現這一點,而無需改變現有的內容頁面。對不起,我沒有明確說明。我認爲它會被理解爲

+0

的@ ViewBag.Title = someStringReference然後你可以在每個控制器只需指定修改根據您定義的稱號。或者您可以將更改直接從模型中拉出,幾個選擇... – Greg 2013-03-21 22:04:45

+0

在您的代碼隱藏中調用this.Page.Title =「特殊內容頁面」更改整個標題嗎? – GregD 2013-03-21 22:07:04

+0

你能指導我走向任何例子嗎?我是新手,不確定「控制器」或「型號」是什麼意思 – brainbolt 2013-03-21 22:08:28

回答

1

有兩種方法取決於您使用的是哪種形式的ASP.Net。

如果你使用MVC,然後簡單的設置了一個名爲「的PageTitle」在您的ViewBag然後引用它在你的母版頁屬性:

在HomeController.cs

@ViewBag.PageTitle = 'Overridden Title' 

在你的主頁:

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

如果您使用的是基於Web窗體應用程序,你可以先通過「混合服務器控制」

暴露HTML標題做到這一點

在你的母版頁,事情改變:

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

然後在你的子頁面,你在上面添加類型引用您的母版頁起來:

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

在你的子頁後面的代碼,你現在可以通過訪問。

Master.MasterTitle.Text = "Overridden Title"; 
+1

我正在使用WebForms。 我收到以下錯誤:由於其保護級別'SiteMaster.MasterTitle'無法訪問 – brainbolt 2013-03-21 22:18:35

+1

@brainbolt在這方面訪問根控件(頭部,主體等)比基本webcontrols有點棘手。看看這個答案,它應該有足夠的信息爲你設置正確的東西 - http://stackoverflow.com/questions/2866208/access-body-element-from-content-page-via-a-nested-master-頁面 – 2013-03-21 22:42:53

+0

好了,所以我添加了以下內容頁面代碼隱藏的page_load:'HtmlTitle title =(HtmlTitle)Master.FindControl(「MasterTitle」);''title.Text =「Overridden Title」;'但我仍然得到'我的網站| Overridden Title'交付給客戶... – brainbolt 2013-03-22 06:14:40

0

對於VS2012,你的孩子如果網頁使用的WebForms,您更改母版頁絕對沒有。一切現在可以通過你的孩子的網頁來完成:

下面的語句去爲你的孩子頁面代碼頂部:

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

更改您的母版頁標記來

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

在你母版。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; 
    } 
} 

在你的 「特殊內容頁」 添加這Page_PreRender

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

這一個讓我瘋狂 - 謝謝你:-) – gchq 2014-11-16 23:36:29

0

在母版頁

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

在子頁面

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

或不使用的內容/佔位

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