简体   繁体   English

使用C#从子页面更新asp.net master的元标记

[英]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. 我正在寻找一种方法来更新已在母版页中进行硬编码的元descriptionkeywords标签。 One of the child pages generates these two meta tags dynamically so, I wanted to add these dynamically. 子页面之一动态生成这两个meta标签,因此,我想动态添加它们。 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. 它工作正常,但是这里的问题是,呈现页面时,它宁愿生成2“ description ”和2“ keyword ”标签,一个是MasterPage(硬编码),另一个是动态添加到页面加载。

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# 我的项目使用C#在asp.net 3.5中

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: 在我的母版页中,我用一个ID(即desckey定义了descriptionkeywords标签,然后在子页面加载中执行了以下操作:

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: 然后,页面开始时不包含任何meta元素,并且如果未触及默认内容,则会写出默认内容,否则将写入更改后的内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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