【问题标题】:How can I add title and meta tags for content pages in a project based on master and content pages (dynamically)如何在基于母版页和内容页的项目中为内容页添加标题和元标记(动态)
【发布时间】:2011-04-26 15:32:50
【问题描述】:

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

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

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);
}

所以不知道为什么Content Page的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

在未来提前感谢

最好的问候

【问题讨论】:

  • 您看到的错误是什么? (在哪一行?)

标签: asp.net master-pages title metatag


【解决方案1】:

您需要将从 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"); 
}

【讨论】:

    【解决方案2】:

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

    <%@ MasterType VirtualPath="PathToYourMasterFile" %>
    

    【讨论】:

      【解决方案3】:

      您可以只使用 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
      

      【讨论】:

        猜你喜欢
        • 2011-08-07
        • 2021-08-07
        • 2014-02-02
        • 2013-02-12
        • 2011-08-20
        • 2015-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多