如何基于母版页和内容页(动态)为项目中的内容页添加标题和元标记
如何在基于母版页和内容页的项目中(动态)添加内容页的标题和元标记?
我对母版页使用了以下方法:
public void SetMetaTags(string title, string description, string keywords)
{
// Get a reference to the HTML Head
HtmlHead headTag = (HtmlHead)Page.Header;
// Set the page title
headTag.Title = title;
// Add a Description meta tag
HtmlMeta metaTag = new HtmlMeta();
metaTag.Name = "Description";
metaTag.Content = description;
headTag.Controls.Add(metaTag);
// Add a Keywords meta tag
metaTag = new HtmlMeta();
metaTag.Name = "Keywords";
metaTag.Content = keywords;
headTag.Controls.Add(metaTag);
}
所以我不知道为什么内容页的 Page_Load
中的以下代码有错误:
protected void Page_Load(object sender, EventArgs e)
{
MasterPage MyMasterPage = (MasterPage)Master;
// Error on this line:
MyMasterPage.SetMetaTags("Title", "description", "keywords");
}
错误是:
Error 17 'System.Web.UI.MasterPage' does not contain a definition for
'SetMetaTags' and no extension method 'SetMetaTags' accepting a first argument of
type 'System.Web.UI.MasterPage' could be found (are you missing a using directive
or an assembly reference?)
C:\Javad\---\AlmasAfzar\AlmasAfzar\AlmasAfzar\Products.aspx.cs 16 26
AlmasAfzar
感谢未来的
问候
How can I add title and meta tags for content pages in a project based on master and content pages (dynamically) ?
I used the below method for master page :
public void SetMetaTags(string title, string description, string keywords)
{
// Get a reference to the HTML Head
HtmlHead headTag = (HtmlHead)Page.Header;
// Set the page title
headTag.Title = title;
// Add a Description meta tag
HtmlMeta metaTag = new HtmlMeta();
metaTag.Name = "Description";
metaTag.Content = description;
headTag.Controls.Add(metaTag);
// Add a Keywords meta tag
metaTag = new HtmlMeta();
metaTag.Name = "Keywords";
metaTag.Content = keywords;
headTag.Controls.Add(metaTag);
}
so I don't know why the following code in Page_Load
of Content Page has an error:
protected void Page_Load(object sender, EventArgs e)
{
MasterPage MyMasterPage = (MasterPage)Master;
// Error on this line:
MyMasterPage.SetMetaTags("Title", "description", "keywords");
}
and the error is:
Error 17 'System.Web.UI.MasterPage' does not contain a definition for
'SetMetaTags' and no extension method 'SetMetaTags' accepting a first argument of
type 'System.Web.UI.MasterPage' could be found (are you missing a using directive
or an assembly reference?)
C:\Javad\---\AlmasAfzar\AlmasAfzar\AlmasAfzar\Products.aspx.cs 16 26
AlmasAfzar
Thanks in future advance
best regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将从 Page.Master 返回的类型转换为母版页的类型,而不是 System.Web.UI.MasterPage。
因此,如果具有 SetMetaTags 方法的母版页类名为 MasterWithMetaTags,则您的 Page_Load 代码需要如下所示:
You need to cast the type returned from Page.Master to be the type of your master page, not System.Web.UI.MasterPage.
So, if your master page class with the SetMetaTags method is named MasterWithMetaTags, your Page_Load code needs to look like this:
没有提及错误是什么,我只能按照您所说的进行。我会确保你的 aspx 文件中有这个指令:
Without any mention of what the error is, I could only go by what you have said. I would make sure you have this directive in your aspx file:
您可以仅使用 Page.Header.Title 作为页面标题。
这是我的。
You can just use Page.Header.Title for page title.
Here is mine.