none
Escaping Single Quote in Literal HTML String

    Question

  • This seems pretty straight forward but I simply cannot find a way to make this work.

    I am basically generating a table using literals. I need to put titles in the TH element so I need to have single quotes around the text so it won't cut off.  The output I'm looking for is:

    <tr><th title = 'This is some hover text'>Title</th>

    However I simply cannot get the single quotes to print out so what I get is this

    <tr><th title = This is some hover text>Title</th>

    So when I hover it only says This 

    I've tried everything I can think of to escape the single quotes to no avail including:

    Literal.Text = "<tr><th title=&#39;" + HoverText + "&#39;>" +  Title + "</th>"

    Literal.Text = "<tr><th title=&apos;" + HoverText + "&apos;>" +  Title + "</th>"

    Literal.Text = "<tr><th title=&#x00027;" + HoverText + "&#x00027;>" +  Title + "</th>"

    Literal.Text = "<tr><th title=\'" + HoverText + "\'>" +  Title + "</th>"

    Any Ideas? I'm developing in C# using Visual Studio. 
    Friday, April 21, 2017 4:43 PM

Answers

  • Ah - It just occurred to me I could add the single quotes to my variable when setting it I tried several different ways but this is what worked:

    HoverText = "\'This is some hover text \'";

    Literal.Text = "<tr><th title=" + HoverText + ">" +  Title + "</th>"


    • Marked as answer by nighthawk75 Friday, April 21, 2017 4:57 PM
    Friday, April 21, 2017 4:57 PM