alp*_*eus 5 asp.net master-pages title
我需要动态设置页面的标题,因此我使用类似于以下的代码:
<%@ 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>
Run Code Online (Sandbox Code Playgroud)
但这会生成重复的标题标签.有什么方法可以解决这个问题吗?谢谢.
编辑:根据以下建议,我现在在我的主页中有以下内容:
<head id="Head1" runat="server">
<title>Default Title</title>
...
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
Run Code Online (Sandbox Code Playgroud)
以及我的主页中的以下内容:
this.Title="xxx";
Run Code Online (Sandbox Code Playgroud)
但我没有得到任何头衔(既没有"默认标题"也没有"xxx").
编辑:没关系.使用该方法使其工作.
Spo*_*boy 10
如果第二个标题标记为空并且在标记的末尾head,那么正在发生的事情是Head控件(注意runat="server")无法在其控件中看到Title控件,因此在末尾添加一个空的Title标记.
这里的大部分答案都是有效的,但是为了实现最小的改变,您可以做的是将以下内容添加到您的头部控制中 -
<title id="Title1" visible="false" runat="server"><%-- hack to turn the auto title off --%></title>
Run Code Online (Sandbox Code Playgroud)
其他选择包括─
runat="server"落头控制,但是这将意味着你不能使用服务器端控件我喜欢隐藏的标题标签,因为它允许您使用ContentPlaceholder方法,该方法将其保留在模板中.它还允许您使用自定义控件来获取标题(在我们的示例中,我们使用第三方库从数据库中提取标题)
.master的标题需要如下所示:
<head runat="server">
<title>Default Title</title>
.. other tags
</head>
Run Code Online (Sandbox Code Playgroud)
然后在Page_Load中的代码隐藏页面中,您编写:
protected void Page_Load(object sender, EventArgs e)
{
this.Title = "My Page Title";
}
Run Code Online (Sandbox Code Playgroud)
小智 5
要避免重复标记,请执行以下操作,无需额外代码.
在您的MasterPage中有这样的事情:
<head>
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
Run Code Online (Sandbox Code Playgroud)
在每个其他页面上,将Title ="Your Title"属性添加到page指令:
<%@ Page Title="Your Title" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Run Code Online (Sandbox Code Playgroud)
这样可以消除重复的标记,并将标题放在代码视图顶部清晰可见的位置.
希望这可以帮助.