Jeff
Jeff

Reputation: 2871

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

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.

Upvotes: 24

Views: 53601

Answers (5)

Avinash Chowdary
Avinash Chowdary

Reputation: 71

I would like to suggest string.format...

...

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

Upvotes: 0

Jan Remunda
Jan Remunda

Reputation: 7930

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

Upvotes: 4

Paul
Paul

Reputation: 9561

You can use:

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

This should work for you

Upvotes: 6

Mayo
Mayo

Reputation: 10812

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?" />

Upvotes: 1

aolde
aolde

Reputation: 2295

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" />

Upvotes: 35

Related Questions