24

Simple question, I know, but I can't seem to find a way to put both single and double quotes into the string of the text property of a Literal in asp.net

<asp:Literal runat="server" id="Literal1" Text="This is my "text", isn't it pretty" />

For example, in the above code snippet. The string closes on the first double-quote around 'text'. I know I could replace them with single quotes (or use all double quotes and wrap the string in single quotes), but I'm not sure how to use both. Escaping the quotes doesn't seem to work.

Setting the string on the code-behind is an option, of course, where I can escape the double quotes, but I've always thought it best to keep static text on the aspx, rather than cluttering the code-behind.

5 Answers 5

35

You can try the HTML enitity for the quotation mark: &quot;

<asp:Literal runat="server" id="Literal1" Text="This is my &quot;text&quot;, isn't it pretty" />
3
  • Yeah, ASP.NET Web Forms files are basically XML files, and the normal XML rules apply to them. In this case, you use standard XML entities to escape the quotation mark. On a related note: to escape an ampersand (&), you would use &amp;.
    – Blixt
    Sep 4, 2009 at 12:15
  • facepalm I'm an idiot. I use these ALL THE TIME. I must have had my stupid flakes this morning, and forgot all about them.
    – Jeff
    Sep 4, 2009 at 12:15
  • This is actually what MS seems to prefer, since if you use IP's suggested technique and run 'Generate Local Resource' (which formats the text) it results in this on the page.
    – Jeff
    Sep 4, 2009 at 12:37
6

You can use:

 <asp:Literal id="literal1" runat="server">This is my "text", isn't it pretty</asp:Literal>

This should work for you

5
  • Does text set this way get picked up by the built-in tools for globalization?
    – Jeff
    Sep 4, 2009 at 12:13
  • 1
    Missing the id, but I like this the best. +1
    – tvanfosson
    Sep 4, 2009 at 12:13
  • Sorry yes, I often leave the ID off if I know I'm never going to need to access it from the code behind, then you don't end up with an enormous id in the generated code.
    – Paul
    Sep 4, 2009 at 12:15
  • 1
    Just tried this style with the built-in resource creator, and it DOES pick up the strings...and promptly sets them to the text property of the literal, HTML-encoding the double quotes.
    – Jeff
    Sep 4, 2009 at 12:19
  • Groovy, I had just discovered the same thing, hope it helps though, was this what you were after?
    – Paul
    Sep 4, 2009 at 12:22
4

you can use double qoutes inside single quotes like this:

<asp:Literal runat="server" id="Literal1" Text='This is my "text", isnt it pretty' />

But if you want to use in text both of them, the best way to do this is in code behind

1

I would suggest escape characters, but I'm not aware of a way to use those inline. Instead, use code to initialize the value.

<asp:Literal runat="server" id="Literal1" Text="" />

...

Literal1.Text = "This is my \"text\", isn't it pretty?";

Alternatively, you can use HTML encoding as suggested elsewhere.

<asp:Literal runat="server" id="Literal1" Text="Isn't &quot;it&quot; pretty?" />
3
  • This is my fallback, of course, but I prefer to keep static text to the front side, not the code-behind.
    – Jeff
    Sep 4, 2009 at 12:13
  • Literal1.Text = System.Web.HttpUtility.HtmlEncode("This is my \"text\", isn't it pretty?"); Sep 4, 2009 at 12:32
  • 1
    I think the downvote came from someone who read the relative portion of my original question: "Setting the string on the code-behind is an option, of course, where I can escape the double quotes, but I've always thought it best to keep static text on the aspx, rather than cluttering the code-behind." Last line of the question.
    – Jeff
    Sep 4, 2009 at 12:34
0

I would like to suggest string.format...

...

Literal1.Text = string.format("{0}","This is my text, isn't it pretty?";

1
  • 2
    A few problems: 1. This question is 2 years old, and has several answers (including an accepted answer), while it's not bad to answer old questions, this was a poor choice to respond to so late. 2. This would have to be done on the code-behind, not the ascx page, which is a possibility I addressed in my question. 3. Your formatting is bad. 4. You left out a closing parenthesis.
    – Jeff
    Apr 23, 2012 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.