Quote pb with databinding

S

Steve B.

Hi,

I've in the code of a webpage a custom class like :

public class MyClass
{
public string prop1 { get; set; }
public string prop2 { get; set; }
}

I've also a webpage that use a repeater. In the page load I've a code like
this :

if(!Page.IsPostBack)
{
List<MyClass> source = new List<MyClass>();
PopulateSource(source); // imaging this fills near 40 MyClass objects.
rptItems.DataSource = source;
rptItems.DataBind();
}

The repeater itemtemplate looks like this :

<asp:Repeater ID="rptItems" runat="server">
<ItemTemplate>
<img title='<%# Eval("prop1") %>' onmousedown='<%# Eval("prop2") %>'
src="img.gif">
Some HTML here
<asp:Label runat="server" Text='<%# Eval("prop1") %>' />
</ItemTemplate>
</asp:Repeater>

Everything is working correctlty untill I have single quote ( ' ) in one of
the property.
example :

prop1 : Hello ' World ' !
prop2: say("Say something very ' usefull ' ");

the outputed result will be :
<img title='Hello ' World ' !' onmousedown='say("Say something very '
usefull ' ")' src="img.gif">
Some HTML here
<span id="xx">Hello ' World ' !</span>


As you can see, having single quote in the attributes will cause problems.
I cannot .Replace("'", "\\'") in the codebehind, because sometimes prop1
will require escaping (in the img title) sometimes not (in the span inner
html).

How can I correctly handle this quotes ?
Thanks in advance,
Steve
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top