LostLord
LostLord

Reputation: 2329

How can I add title and meta tags for content pages in a project based on master and content pages (dynamically)

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

Upvotes: 0

Views: 3127

Answers (3)

Kadir
Kadir

Reputation: 3224

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

Upvotes: 0

Reed Rector
Reed Rector

Reputation: 502

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

Upvotes: 1

Gabriel McAdams
Gabriel McAdams

Reputation: 58251

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" %>

Upvotes: 0

Related Questions