gpt4 book ai didi

asp.net - 使用 ASP.NET 母版页复制标题标签

转载 作者:行者123 更新时间:2023-12-04 16:22:40 25 4
gpt4 key购买 nike

我需要动态设置页面的标题,因此我使用类似于以下的代码:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="~/about.aspx.cs" Inherits="Default" %>
<%@ Register Assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajaxToolkit" %>
<%@ MasterType VirtualPath="MasterPage.master" %>
<%@ OutputCache Duration="43200" VaryByParam="*" Location="Server" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<title><%=pageTitle%></title>
</asp:Content>

但这会生成重复的标题标签。有什么办法可以解决这个问题吗?谢谢。

编辑:按照下面的建议,我现在在我的 MasterPage 中有以下内容:
<head id="Head1" runat="server">
<title>Default Title</title>
...
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>

以及我的主页中的以下内容:
    this.Title="xxx";

但我没有得到任何标题(既不是“默认标题”也不是“xxx”)。

编辑:没关系。使用这种方法让它工作。

最佳答案

如果第二个标题标签为空并且在 head 的末尾标签,那么发生的事情是 Head 控件(注意 runat="server" )在它的控件中看不到 Title 控件,因此在末尾添加了一个空的 Title 标签。

这里的大多数答案都是有效的,但为了最小的改变,你可以做的是 add the following to your head control ——

<title id="Title1" visible="false" runat="server"><%-- hack to turn the auto title off --%></title>

其他选项包括——
  • runat="server"关闭头部控件,但这意味着您不能使用服务器端控件
  • 使用 Title 控件并以编程方式设置它,就像您的解决方案一样。

  • 我喜欢隐藏的标题标签,因为它允许您使用 ContentPlaceholder 方法,将它保存在模板中。它还允许您使用自定义控件来获取标题(在我们的例子中,我们使用 3rd 方库从数据库中提取标题)

    See this blog post by Phil Haack for the background on this.

    关于asp.net - 使用 ASP.NET 母版页复制标题标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2102321/

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