0

I've been through a dozen blogs tonight looking for a solution for this problem. I am populating pages dynamically (shopping cart) and I grab the product name and descriptionfrom SQL Server then use that data to populate the page title, keywords and description meta tags. This is usually simple until you get a product with special characters. In this case and apostrophe ('). An apostrophe will display as expected in the <title> tag but in the meta tags I am gettimg the Html encoded equiv.

This is what I want: <meta name="keywords" content="3',5'-Mahogany" />
This is what I get: <meta name="keywords" content="3&#39;,5&#39;-DIMETHOXY-4&#39;-Mahogany" />

I tried HtmlDecode but it didn't work.
Here are a couple of other methods I have tried.

string prodName = (string)myData.GetProdName();
Page.Title = prodName;
Page.MetaKeywords = prodName;
Page.MetaDescription = prodName;

HtmlMeta kw = new HtmlMeta();
kw.HttpEquiv = "keywords";
kw.Content = String.Format("{0}", myData.GetProdName());
this.Header.Controls.Add(kw);

I'm hoping another set of eyes will solve this one :)

Thanks All!

Edited by bill_kearns: typo

1
Contributor
1
Reply
2
Views
4 Years
Discussion Span
Last Post by bill_kearns
0

So I was able to get this solved with some help from both Matt Fei and Scott Gu over at Microsoft and thought I'd pass that on here in case anyone else needs a fix someday.

/*** In my class file CustomHtmlMeta.cs ***/
public class CustomHtmlMeta : HtmlMeta
{
    protected override void RenderAttributes(System.Web.UI.HtmlTextWriter writer)
    {
        base.RenderAttributes(new CustomHtmlTextWriter(writer));
    }
}

public class CustomHtmlTextWriter : HtmlTextWriter
{
    public CustomHtmlTextWriter(HtmlTextWriter writer) : base(writer)
    { }

    public override void WriteAttribute(string name, string value, bool fEncode)
    {
        if (string.Compare(name, "content", true) == 0)
            base.WriteAttribute(name, value, false);
        else
            base.WriteAttribute(name, value, fEncode);
    }
}


/***  In my .aspx code behind page  ***/
String prodName = (string)myData.GetChemName();
CustomHtmlMeta kw = new CustomHtmlMeta();
        //kw.HttpEquiv = "keywords";
        kw.Name = "keywords";
        kw.Content = prodName;
        kw.Attributes.Add("content", kw.Content.ToString());
        this.Header.Controls.Add(kw);

        CustomHtmlMeta desc = new CustomHtmlMeta();
        //desc.HttpEquiv = "description";
        desc.Name = "description";
        desc.Attributes.Add("content", prodName + keywordSynonyms);
        this.Header.Controls.Add(desc);

Edited by bill_kearns: typo

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.