views:

24

answers:

3

how can i add title and meta tags for content pages in a project base on master and content page(dinamically) ?

i used the blow 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 blow code in page_Load Of Content Page Have An Error :

protected void Page_Load(object sender, EventArgs e)
{
    MasterPage MyMasterPage = (MasterPage)Master;

    MyMasterPage.SetMetaTags("Title", "description", "keywords"); *
}

and the error is :(in line *)

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

A: 

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" %>
Gabriel McAdams
thanks 4 your attentons - i updated my question...
LostLord
+1  A: 

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"); 
}
Reed Rector
A: 

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
kad1r