Asp.net 使用母版页和内容占位符创建子标题
我有一个只有几页的网站,我希望这些页面的标题是:Asp.net 使用母版页和内容占位符创建子标题,asp.net,master-pages,contentplaceholder,Asp.net,Master Pages,Contentplaceholder,我有一个只有几页的网站,我希望这些页面的标题是: Foo - 1st Page Foo - 2nd Page Foo - 3rd Page 我已使用以下代码创建了母版页: <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Foo.master.cs" Inherits="Foo" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E
Foo - 1st Page
Foo - 2nd Page
Foo - 3rd Page
我已使用以下代码创建了母版页:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Foo.master.cs" Inherits="Foo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Foo - <asp:ContentPlaceHolder ID="SubTitle" runat="server"></asp:ContentPlaceHolder></title>
</head>
<body>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
我做错了什么?原来这个问题已经被问到了:
在阅读了那里的答案后,我发现从母版页的
头元素中删除runat=“server”
成功了。您不想从头中删除runat=“server”
;这会造成其他问题
使用以下方法,这是Visual Studio 2012中ASP.Net Web窗体应用程序中的默认方法
<!-- Master Page -->
<head runat="server">
<title>Foo - <%: Page.Title %></title>
</head>
<!-- Content Page/xxx.aspx -->
<%@ Page Title="Home Page" ...>
富-
您认为生成的标记是什么?(无论如何,对我来说,这是一个不寻常的占位符用法。)我想知道一个更好的解决方案是否是在代码中设置页面的Title属性。@ThatBlairGuy-我在我的问题中添加了html源代码。Visual Studio的母版页模板在母版页的标题标记中有一个名为title
的内容占位符。我刚刚在它前面添加了Foo-
文本,并将其重命名为SubTitle
。非常好。非常感谢。就我而言,缺少runat=“server”
不会导致问题(这只是一小部分测试页面,在标题中没有其他内容),但我采纳了您的建议,因为它使代码更简单。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
1st Page
</title></head>
<body>
<a href="Page2.aspx">Page 2</a>
</body>
</html>
<!-- Master Page -->
<head runat="server">
<title>Foo - <%: Page.Title %></title>
</head>
<!-- Content Page/xxx.aspx -->
<%@ Page Title="Home Page" ...>