简体   繁体   中英

Update meta tags of asp.net master from the child page with C#

I am looking for a way to update my meta description and keywords tag that has been hard-coded in my master page. One of the child pages generates these two meta tags dynamically so, I wanted to add these dynamically. Here is the code of the child page that I am currently using to add the key and description to the page.

protected void Page_Load(object sender, EventArgs e)
{
    this.Page.Title = lblPackTitle.Text;
    System.Web.UI.HtmlControls.HtmlMeta metaTagKey = new System.Web.UI.HtmlControls.HtmlMeta();
    metaTagKey.Name = "Keywords";
    metaTagKey.Content = "This is my keyword text";
    this.Header.Controls.Add(metaTagKey);
    System.Web.UI.HtmlControls.HtmlMeta metaTagDesc = new System.Web.UI.HtmlControls.HtmlMeta();
    metaTagDesc.Name = "description";
    metaTagDesc.Content = "This is my description text";
    this.Header.Controls.Add(metaTagDesc);
}

It works fine, but the issue here is that when the page is rendered, it would rather generate 2 " description " and 2 " keyword " tags, the one of the MasterPage(hard-coded) and the other, that was added dynamically on page load.

So, is there any way to just update the existing meta tags already in the masterpage dynamically, or just remove those on masterpage and add only the dynamically added (from child page), every time the child page is rendered?

My project is in asp.net 3.5 with C#

Well, I found a solution, and as it looks to be working fine after rendering, I hope it works ok on every case.

In my master page, I defined my description and keywords tag with an ID, namely desc and key and after that on my child page load I did:

protected void Page_Load(object sender, EventArgs e)
{
    this.Page.Title = lblPackTitle.Text;
    System.Web.UI.HtmlControls.HtmlMeta metaTagKey = new System.Web.UI.HtmlControls.HtmlMeta();
    metaTagKey.Name = "Keywords";
    metaTagKey.Content = "This is my keyword text";
    this.Header.Controls.Add(metaTagKey);
    System.Web.UI.HtmlControls.HtmlMeta metaTagDesc = new System.Web.UI.HtmlControls.HtmlMeta();
    metaTagDesc.Name = "description";
    metaTagDesc.Content = "This is my description text";
    this.Header.Controls.Add(metaTagDesc);
    //----------------------------Added here-----------------------------
    Control ctrlKeyMeta = this.Header.FindControl("key");
    Control ctrlDescMeta = this.Header.FindControl("desc");
    ctrlKeyMeta.Visible = false;
    ctrlDescMeta.Visible = false;
}

Expose the meta content items as properties with a default value and render them based on that (in fact, you don't even need to do this as the page will expose the properties, you might want top-level master defaults though, which look to the Page's properties). The page then starts with no meta elements, and the default content is written out if they're untouched otherwise the altered content gets written:

public MetaTitle { get; set; }
public MetaDescription { get; set; }
public MetaKeywords { get; set; }

...

var tag = new HtmlMeta();
tag.Name = "description";
tag.Content = MetaDescription;
this.Header.Controls.Add(tag);

The properties can be set further on up the request lifecycle pipeline, and overwritten by individual pages. No duplication. The key, then, it to not hardcode the defaults, but give some defaults along with flexibility of those same things.

Furtherance Question: This adds the Meta Description as the last line item within the Head Section. Does it really make a difference if its last within the Head and not at the top?

      protected void Page_Load(object sender, EventArgs e)
    {
        HtmlHead header1 = (HtmlHead)Master.FindControl("head1");

        header1.Controls.Remove(header1.FindControl("desc1"));

        HtmlMeta description = new HtmlMeta();
        //System.Web.UI.HtmlControls.HtmlMeta description = new System.Web.UI.HtmlControls.HtmlMeta();

        description.Name = "description";
        description.Content = "We are a family owned and operated painting company. We serve homeowners in Northern Virginia and Washington DC";

        this.Header.Controls.Add(description);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM