代码之家  ›  专栏  ›  技术社区  ›  User

ASP.NET:母版页中有公共页标题,每个页都添加特定于页的标题?

  •  0
  • User  · 技术社区  · 14 年前

    我的母版页看起来像:

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

    内容页看起来像:

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

    这是通过在页面上放置特定于内容页面的标题来实现的(在本例中为“我的页面”)。现在,我想在母版页的标题中为网站名称添加一个全局前缀。所以我想要:

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

    但是,当我这样做时,这些内容页仍然没有在平铺中显示“example.com”,就好像它被忽略了一样。

    为什么会发生这种事,我如何才能做到这一点?

    5 回复  |  直到 14 年前
        1
  •  3
  •   Martin    14 年前

    在母版页的代码隐藏中尝试此操作:

    void MasterPage_PreRender(object sender, EventArgs e)
    {
        Page.Title = "Example.com - " + Page.Title;
    }
    
        2
  •  2
  •   Daryl Faulkner    12 年前

    从母版页中删除标题标记并使用martin提供的代码。现在,在内容页中,在文件顶部的页面标记中设置标题,如下所示:

    <%@ Page ... Title="Contact" %>
    
        3
  •  1
  •   Jake    7 年前

    我发现最容易接受的方法是使用多个 ContentPlaceHolder 控制。

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

    注意里面的任何其他内容 <title> ,包括空格,被去掉。之间的任何间距 内容占位符 内容需要在控件内完成。

    我也用过 <asp:Literal runat="server">Example.com: </asp:Literal> 当我不想向内容页公开占位符时。

        4
  •  0
  •   Dudu    14 年前

    移除 “runat=”服务器“ 但我不确定,试试看

    <head>
        <title>
            Example.com:
            <asp:ContentPlaceHolder ID="PageTitlePlaceHolder" runat="server" />
        </title>
    
        5
  •  0
  •   Moshe L    12 年前

    两种选择。 一个是删除 runat=server <head> (杰弗里杜)

    二是使用page.title作为字符串。(马丁)

    我更喜欢使用我在某个地方写的对象,为我使用的任何页面添加facebook标题、搜索引擎描述等。此对象将页标题存储在三个字符串中-一个在前,一个是页标题,一个在后。

        6
  •  0
  •   xolani maphumulo    5 年前

    在母版页中,放置

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

    其中website name.com是网站的域名。

    然后在每一个内容页上,放一个标题。