如何基于母版页和内容页(动态)为项目中的内容页添加标题和元标记

发布于 2024-09-27 16:05:13 字数 1337 浏览 4 评论 0原文

如何在基于母版页和内容页的项目中(动态)添加内容页的标题和元标记?

我对母版页使用了以下方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

古镇旧梦 2024-10-04 16:05:13

您需要将从 Page.Master 返回的类型转换为母版页的类型,而不是 System.Web.UI.MasterPage。

因此,如果具有 SetMetaTags 方法的母版页类名为 MasterWithMetaTags,则您的 Page_Load 代码需要如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    MasterWithMetaTags MyMasterPage = (MasterWithMetaTags)Master;
    MyMasterPage.SetMetaTags("Title", "description", "keywords"); 
}

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:

protected void Page_Load(object sender, EventArgs e)
{
    MasterWithMetaTags MyMasterPage = (MasterWithMetaTags)Master;
    MyMasterPage.SetMetaTags("Title", "description", "keywords"); 
}
不寐倦长更 2024-10-04 16:05:13

没有提及错误是什么,我只能按照您所说的进行。我会确保你的 aspx 文件中有这个指令:

<%@ MasterType VirtualPath="PathToYourMasterFile" %>

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:

<%@ MasterType VirtualPath="PathToYourMasterFile" %>
筱果果 2024-10-04 16:05:13

您可以仅使用 Page.Header.Title 作为页面标题。
这是我的。

#region meta tags and title

                Page.Header.Title = dtArticleDetails.Rows[0]["title"].ToString();

                string Keywords = dtArticleDetails.Rows[0]["keywords"].ToString();
                string Description = dtArticleDetails.Rows[0]["description"].ToString();

                HtmlMeta keywordss = new HtmlMeta();

                HtmlHead head = (HtmlHead)Page.Header;
                keywordss.Name = "keywords";
                keywordss.Content = Keywords;
                head.Controls.Add(keywordss);

                HtmlMeta desc = new HtmlMeta();
                desc.Name = "description";
                desc.Content = Description;

                HtmlHead head2 = (HtmlHead)Page.Header;
                head2.Controls.Add(desc);

                #endregion

You can just use Page.Header.Title for page title.
Here is mine.

#region meta tags and title

                Page.Header.Title = dtArticleDetails.Rows[0]["title"].ToString();

                string Keywords = dtArticleDetails.Rows[0]["keywords"].ToString();
                string Description = dtArticleDetails.Rows[0]["description"].ToString();

                HtmlMeta keywordss = new HtmlMeta();

                HtmlHead head = (HtmlHead)Page.Header;
                keywordss.Name = "keywords";
                keywordss.Content = Keywords;
                head.Controls.Add(keywordss);

                HtmlMeta desc = new HtmlMeta();
                desc.Name = "description";
                desc.Content = Description;

                HtmlHead head2 = (HtmlHead)Page.Header;
                head2.Controls.Add(desc);

                #endregion
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文