2013-03-21 91 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 
相关问题