Important Notice from AspDotNetStorefront
It is with dismay that we report that we have been forced, through the action of hackers, to shut off write-access to this forum. We are keen to leave the wealth of material available to you for research. We have opened a new forum from which our community of users can seek help, support and advice from us and from each other. To post a new question to our community, please visit: http://forums.vortx.com
Results 1 to 2 of 2

Thread: How can I change the meta description tag contents in CodeBehind?

  1. #1
    omairkha is offline Member
    Join Date
    Aug 2011
    Posts
    89

    Question How can I change the meta description tag contents in CodeBehind?

    Hi,

    For one of my static pages I need to dynamically change the meta description from the CodeBehind file (myPage.aspx.cs)

    I did some research and found that the way to do this in .NET 3.5 is:
    Code:
    protected void Page_Load(object sender, System.EventArgs e)
    {
       <..other existing code..>			
    			
       //set meta tag description
       HtmlMeta hm1 = new HtmlMeta();
       hm1.Name = "description";
       hm1.Content = "Test Description";
    					
       Page.Header.Controls.Add(hm1);
    
    }

    However, instead of modifying the existing tag, it creates a duplicate <meta name="description" content=""> just before the </header> tag with content being populated from ASPDNSF. The description I am inputting in the hm1.Content is ignored.

    This only works if I delete the <meta name="description" content=""> tag from my template.master. With the meta tag deleted from the template everything works fine.

    But since other pages need to pull the description from ASPDNSF I cannot just simply delete the meta tag in template.master.

    I have also tried putting an if statement in the template.master file to detect the filename and only write the meta tag if the page is not myPage.aspx but I received an error saying that code blocks are not allowed in that section.

    How can I edit the description in the aspx.cs file?

    Thanks in advance,
    Last edited by omairkha; 09-30-2011 at 09:34 PM.

  2. #2
    omairkha is offline Member
    Join Date
    Aug 2011
    Posts
    89

    Red face

    I actually found the code that ASPDNSF uses in SkinBase.cs to do this from the data entered in the admin for topics etc.

    so I copied that and modified it a little for my aspx.cs file and everything works great now!

    Code:
    protected void Page_Load(object sender, System.EventArgs e)
    {
       <..other existing code..>
    
       if (this.Header != null)
       {
          foreach (Control c in this.Header.Controls)
          {
             HtmlMeta hm1 = c as HtmlMeta;
             if (hm1 != null && hm1.Name.Equals("description", StringComparison.InvariantCultureIgnoreCase))
             {
                hm1.Content = "enter new description here";
              }
           }
        }
    }