gpt4 book ai didi

c# - 以编程方式更改页面标题的 MasterPage 部分

转载 作者:行者123 更新时间:2023-11-30 16:19:01 26 4
gpt4 key购买 nike

我想覆盖 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% 的时间里都很好。但是,我希望能够更改交付给客户的整个标题,使其不包括母版页标题中指定的部分,即:<title>Special content page</title>

是否可以通过编程方式从内容页面更改整个页面标题?我不想更改 MasterPage 的配置或使用不同的 MasterPage。

编辑:我还需要能够在不更改现有内容页面的情况下实现这一点。对不起,我没有明确说明这一点。我以为它会被理解

最佳答案

将母版页标记更改为

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

在你的 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;
}
}

在您的“特殊内容页面”中将此添加到 Page_PreRender

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

关于c# - 以编程方式更改页面标题的 MasterPage 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15559032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com