【发布时间】:2011-10-09 09:04:17
【问题描述】:
我有一个母版页,其中定义了元标记名称和描述。我不能让头部使用runat="server",因为我在 URL 路由方面遇到了一些问题,如果我这样做的话。
我想动态更改网站中某个子页面的元描述。我试过这个:
HtmlHead headTag = (HtmlHead)this.Header;
HtmlMeta pageMetaTag = new HtmlMeta();
pageMetaTag.Name = "Description";
pageMetaTag.Content = "Test";
headTag.Controls.Add(pageMetaTag);
但我在 Add 行收到一个错误,说:
Object reference not set to an instance of an object.
在头部内容中为子页面保留<head runat="server"></head> 时,页面会运行但元描述标签不会被覆盖。元描述与母版页的相同......
请帮我解决这个问题....
使用:VS2010 中的 ASP.NET 4
更新(子页面看起来很像):
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Profile.aspx.cs" Inherits="Profile"%>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajax" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<head runat="server"></head>
<style type="text/css" runat="server">
.lnkbtn
{
color: #000080;
cursor:pointer;
}
.lnkbtn:hover
{
color: #800000;
text-decoration: underline;
}
</style>
<link rel="stylesheet" type="text/css" href='<%= ResolveUrl("~/css/back.css") %>'/>
<script type="text/javascript" src='<%= ResolveUrl("~/css/front.js") %>'></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
// contains
// update panel
// webpartmanager
// webpartzones
</asp:Content>
更新(Head 嵌套在 head 标签内)
使用Page.MetaDescription = "Hi How are you";时,页面来源如下:
<head>
<titleMaster Page Title</title>
<meta name="Keywords" content="Master page keywords" />
<meta name="description" content="Master page meta description." />
.
.
.
<head><title>Child page Title</title>
<meta name="description" content="Hi How are you" /></head>
.
.
.
</head>
【问题讨论】:
-
你用的是什么asp.net版本?
-
好的,在那种情况下应该很容易,请参阅下面的答案
标签: c# asp.net html master-pages meta-tags