views:

1197

answers:

4

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.

+6  A: 

You can use:

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

This should work for you

Paul
Does text set this way get picked up by the built-in tools for globalization?
Jeff
Missing the id, but I like this the best. +1
tvanfosson
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
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
Groovy, I had just discovered the same thing, hope it helps though, was this what you were after?
Paul
+4  A: 

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" />
Manticore
Blixt
*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
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
A: 

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?" />
Mayo
This is my fallback, of course, but I prefer to keep static text to the front side, not the code-behind.
Jeff
Seriously on the downvote?
Mayo
+1 now you're back to a neutral position :P
Raúl Roa
Literal1.Text = System.Web.HttpUtility.HtmlEncode("This is my \"text\", isn't it pretty?");
HollyStyles
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
+1  A: 

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

Jan Remunda