Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET: single and double quotes inside the text property of a Literal

Tags:

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.

like image 899
Jeff Avatar asked Sep 04 '09 12:09

Jeff


People also ask

How can you include a double quote character into a literal string?

If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.

Which literal is a single character surrounded by single or double quotes?

A string literal is bracketed by either single quotes ( ' ) or double quotes ( " ).

What are the differences between single and double quoted literal strings?

A single-quoted string does not have variables within it interpreted. A double-quoted string does. Also, a double-quoted string can contain apostrophes without backslashes, while a single-quoted string can contain unescaped quotation marks.

How do you put a double quote in a string?

To place quotation marks in a string in your code In Visual Basic, insert two quotation marks in a row as an embedded quotation mark. In Visual C# and Visual C++, insert the escape sequence \" as an embedded quotation mark.


2 Answers

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" />
like image 147
aolde Avatar answered Sep 17 '22 21:09

aolde


You can use:

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

This should work for you

like image 42
Paul Avatar answered Sep 18 '22 21:09

Paul