Double Quote (") in String

  • Thread starter Thread starter Kali
  • Start date Start date
K

Kali

I need to use a double quote (") in the value and text of a listitem. How
can this be done without having to use " ?
For example, <asp:listitem value="2" x 4"" text="2" x 4""/> doest work. I
don't want to use &quot;, is this possible with a @ or something?
 
Well, &quot; is really the correct way to do this. But, there are a few
other options:

First, you only need the text and value attributes of a ListItem when you
enter their values in the Properties Window of VS while in source code view,
but even then you can use single quotes for the property boudaries, as in:

<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text='3"' Value='3"'>2"</asp:ListItem>
</asp:DropDownList>

This allows you to use the double quotes in the property value, without them
screwing up the reading of the markup.

But, you don't even have to write the code that way to achieve the same
result. If you were to use the SmartTask option in the Designer, and edit
the listitems that way, you'll see the code gets written like this:

<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>2&quot;</asp:ListItem>
</asp:DropDownList>

But, because the text/value isn't being stored in a property, per se, you
can replace the &quot; with ", as in:

<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>2"</asp:ListItem>
</asp:DropDownList>

-Scott
 
I need to use a double quote (") in the value and text of a listitem.
How can this be done without having to use &quot; ?
For example, <asp:listitem value="2" x 4"" text="2" x 4""/> doest
work. I don't want to use &quot;, is this possible with a @ or
something?

There is no way, in XML, to escape in a tag. The ASPX page is an XHTML
document, so no.

You can bind the list item to strings with quotes, however. You can also
use two single quotes for inches or switch to single quotes to set off the
string (at least I think that will work), but binding is a better option
than hard coding list items.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Another options is you can initialize the value in code. Here you can use
\" for double quotes.

protected void DropDownList1_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Add("2\"");
DropDownList1.Items.Add("3\"");
}
}
 
Thanks. I am using C#, however, not inserting the items in code. Can
<asp:listitem value="2\"" be used?
 
Kali said:
Thanks. I am using C#, however, not inserting the items in code. Can
<asp:listitem value="2\"" be used?

No. The \" escape sequence is just for C# code. As I mentioned though, the
simplest answer for you is to use single quotes to surround your value,
which can then have double quotes as in:

<asp:listitem value='2"'>

-Scott
 
Back
Top