Single and Double Quotes in ASP.NET

  • Thread starter Thread starter Roger Helliwell
  • Start date Start date
R

Roger Helliwell

Hi Everyone,

I have a DataGrid which contains various columns, one of which is an
image which comes from the data source. I'd like to bind the HTML
'alt' setting as well with text from the data source. A problem occurs
when the text in the data source has a single quote however.

For example, if the text in the data source is "John Doe's tractor",
only "John Doe" appears in the IMG tag. (The single quote terminated
the string.)

My code is below. I've played around with rearranging the quotes, but
no success. Does anyone know a way around this? (Incidentally, I even
tried changing "John Doe's" in my data source to John Doe's but
the result is the same.)

<ItemTemplate>

<IMG alt='<%# DataBinder.Eval(Container.DataItem, "Description")%>'
src='<%# "thumbs/" + DataBinder.Eval(Container.DataItem,
"Snapshot")%>' width="100px">

</ItemTemplate>

Thanks,
Roger
 
Roger said:
Hi Everyone,

I have a DataGrid which contains various columns, one of which is an
image which comes from the data source. I'd like to bind the HTML
'alt' setting as well with text from the data source. A problem occurs
when the text in the data source has a single quote however.

For example, if the text in the data source is "John Doe's tractor",
only "John Doe" appears in the IMG tag. (The single quote terminated
the string.)

My code is below. I've played around with rearranging the quotes, but
no success. Does anyone know a way around this? (Incidentally, I even
tried changing "John Doe's" in my data source to John Doe&apos;s but
the result is the same.)

<ItemTemplate>

<IMG alt='<%# DataBinder.Eval(Container.DataItem, "Description")%>'
src='<%# "thumbs/" + DataBinder.Eval(Container.DataItem,
"Snapshot")%>' width="100px">

</ItemTemplate>

Thanks,
Roger

Replace every single quote with &apos; on databinding:
<%# Replace(DataBinder.Eval(Container.DataItem,
"Description"),"'","&apos;")%>
 
Roger,

Try <IMG alt="<%# DataBinder.Eval(Container.DataItem, \"Description\")%>"

Eliyahu
 
Replace every single quote with &apos; on databinding:
<%# Replace(DataBinder.Eval(Container.DataItem,
"Description"),"'","&apos;")%>

Makes sense in theory, but since I'm enclosing everything within
single quotes, the "'" string still causes a syntax error.
I did finally work out a solution though, although it's a hack. In my
Page_Load event, I iterate through all rows in my DataSet and replace
every single quote with "&rsquo;" (That's the right-angled single
quote). It seems the <img alt> attribute doesn't seem to mind that.

Roger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top