how to escape single quotes in a DataBinder on the client - side

I have a link in a dataList like so:

<a href="javascript:openMap('showRoomLocation.aspx?room=<%# DataBinder.Eval(Container.DataItem, "vchrSRRoomNum") %>');" class="RedBold"><%# DataBinder.Eval(Container.DataItem, "vchrSRRoomNum") %></a>


Is there a way to escape single quotes in the room number in the href?
LVL 3
Paul KahlAsked:
Who is Participating?
 
rundkaasConnect With a Mentor Commented:
How about this:
<a href="javascript:openMap('showRoomLocation.aspx?room=<%# DataBinder.Eval(Container.DataItem, "vchrSRRoomNum").ToString().Replace("'","\\'") %>');" class="RedBold"><%# DataBinder.Eval(Container.DataItem, "vchrSRRoomNum") %></a>
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you have to duplicate them.

0
 
Paul KahlAuthor Commented:
what i'm looking for is this:

if
<a href="javascript:openMap('showRoomLocation.aspx?room=<%# DataBinder.Eval(Container.DataItem, "vchrSRRoomNum") %>');" class="RedBold"><%# DataBinder.Eval(Container.DataItem, "vchrSRRoomNum") %></a>

would print out

<a href="javascript:openMap('showRoomLocation.aspx?room=men's restroom');" class="RedBold">men's restroom</a>

then I want it to print out

<a href="javascript:openMap('showRoomLocation.aspx?room=men\'s restroom');" class="RedBold">men's restroom</a>

how do I do that, on the fly?
0
 
Paul KahlAuthor Commented:
That's it exactly! Thank you!
0
 
Paul KahlAuthor Commented:
well - almost it: one small change:

<a href="javascript:openMap('showRoomLocation.aspx?room=<%# DataBinder.Eval(Container.DataItem, "vchrSRRoomNum").ToString().Replace("'","\'") %>');" class="RedBold"><%# DataBinder.Eval(Container.DataItem, "vchrSRRoomNum") %></a>


replaced \\ with \ in the replace command.

Thank you a ton!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.