In this case, I will need to escape the single quote, otherwise the page will have an error. I have tried two different solutions, but both of them didn't work for me.
1. I tried to go through each item in the DataTable and replaced each single quote with double quote. I can't do that because I am using the same thing in another place where I am actually displaying the name, and is that case It would
show double quote. Which is VERY BAD.
2. I tried to replace every single quote with ' thinking that when it binds it would actually understand it as a single quote. But I was wrong. It actually puts '. Which is really bad.
Now I am wondering if there is a way I could replace the name with double quote right before I put it in to the JavaScript above. So somehow replace all the single quotes in DataBinder.Eval(Container.DataItem, "Name") with double quotes.
So this way, I could handle both cases, where I could just bind the name to show the name and put the name in the JavaScript as well. I HOPE someone can help me with this. Thank you very much.
So now all you need to do is do a replace from within javascript, replacing ' with \' . Do a quick search in google for javascript string replace and you get plenty of results. You may need to create a function and call this from the onmouseover though.
I miss a time when programmers were real programmers, and not drag and drop artists!!
Hi, I am not too good with JavaScript. Can you help me how this is done? I tried to use the replace function in JavaScript, but it doesn't seem like it's doing anything at all. Probably I am doing it wrong. Here is what I did:
LudovicoVan way looks better than the javascript method.
Well, to be clear, there is no javascript method here that would work. The problem indeed is how to write javascript from server code, that is we are "on the server" there, trying to render javascript code, and we cannot ask javascript to escape itself,
so to say. We are "generating" it instead, and we need generate the proper code to begin with.
Sanch
Member
625 Points
131 Posts
How to Escape single quote in JavaScript?
Nov 22, 2006 08:20 PM|LINK
Hi,
I am trying to escape single quotes within JavaScript. Below is part of the code where I am Binding to a a DataGrid:
<asp:TemplateColumn SortExpression="ManualInsert"> <ItemTemplate> <a href="#" onmouseover="window.status=''; return true;" onclick="win = window.open('statementReinsert.aspx?docID=<%# DataBinder.Eval(Container.DataItem, "DocumentID") %>&Name=<%# DataBinder.Eval(Container.DataItem, "Name") %>', 'ManualInsert', 'titlebar=0,status=0');">Manual Insert</a> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateColumn>This is where I will have a problem:
<%# DataBinder.Eval(Container.DataItem, "Name") %>
The Name could be for an example Jack O'Neil.
In this case, I will need to escape the single quote, otherwise the page will have an error. I have tried two different solutions, but both of them didn't work for me.
1. I tried to go through each item in the DataTable and replaced each single quote with double quote. I can't do that because I am using the same thing in another place where I am actually displaying the name, and is that case It would show double quote. Which is VERY BAD.
2. I tried to replace every single quote with ' thinking that when it binds it would actually understand it as a single quote. But I was wrong. It actually puts '. Which is really bad.
Now I am wondering if there is a way I could replace the name with double quote right before I put it in to the JavaScript above. So somehow replace all the single quotes in DataBinder.Eval(Container.DataItem, "Name") with double quotes. So this way, I could handle both cases, where I could just bind the name to show the name and put the name in the JavaScript as well. I HOPE someone can help me with this. Thank you very much.
Sanch
jagdipa
Star
8583 Points
2015 Posts
Re: How to Escape single quote in JavaScript?
Nov 23, 2006 08:09 AM|LINK
The escape character is \ i.e.
alert('this is an example - hello O\'Neil');
So now all you need to do is do a replace from within javascript, replacing ' with \' . Do a quick search in google for javascript string replace and you get plenty of results. You may need to create a function and call this from the onmouseover though.
Sanch
Member
625 Points
131 Posts
Re: How to Escape single quote in JavaScript?
Nov 23, 2006 05:44 PM|LINK
Hi, I am not too good with JavaScript. Can you help me how this is done? I tried to use the replace function in JavaScript, but it doesn't seem like it's doing anything at all. Probably I am doing it wrong. Here is what I did:
<a href="#" onmouseover="window.status=''; return true;" onclick="win = window.open('statementReinsert.aspx?docID=<%# DataBinder.Eval(Container.DataItem, "DocumentID") %>&Name=<%# DataBinder.Eval(Container.DataItem, "Name").replace("/'/g", "TEST") %>', 'ManualInsert', 'titlebar=0,status=0');">Manual Insert</a>I just tried to see if it's replacing anything, but it doesn't. What am I doing wrong here?
LudovicoVan
Star
9692 Points
1935 Posts
Re: How to Escape single quote in JavaScript?
Nov 24, 2006 06:36 AM|LINK
Hello, binding syntax runs on the server-side, so you are mixing languages there.
It might look like this in VB:
<%# DataBinder.Eval(Container.DataItem, "Name").ToString().Replace("'", "\'") %>
And something like this in C#:
<%# DataBinder.Eval(Container.DataItem, "Name").ToString().Replace("'", "\\'") %>
Not tested, anyway you get the idea.
Hope this helps. -LV
jagdipa
Star
8583 Points
2015 Posts
Re: How to Escape single quote in JavaScript?
Nov 24, 2006 08:49 AM|LINK
LudovicoVan way looks better than the javascript method. Try it and let us know if it works.
Jag
harshal_shra...
Member
319 Points
237 Posts
Re: How to Escape single quote in JavaScript?
Nov 24, 2006 09:31 AM|LINK
Sanch
Member
625 Points
131 Posts
Re: How to Escape single quote in JavaScript?
Nov 24, 2006 01:42 PM|LINK
Hello Guys,
Thank you sooooo much for all of your help. It worked. Thank you so much. :)
LudovicoVan
Star
9692 Points
1935 Posts
Re: How to Escape single quote in JavaScript?
Nov 24, 2006 02:15 PM|LINK
Well, to be clear, there is no javascript method here that would work. The problem indeed is how to write javascript from server code, that is we are "on the server" there, trying to render javascript code, and we cannot ask javascript to escape itself, so to say. We are "generating" it instead, and we need generate the proper code to begin with.
Hope this sheds more light. -LV
jhallal
Participant
1547 Points
299 Posts
Re: How to Escape single quote in JavaScript?
Aug 25, 2010 09:36 AM|LINK
hi, check the following:
http://weblogs.asp.net/jhallal/archive/2010/08/24/escape-single-quotes-within-javascript.aspx
Note: make sure to replace the single quote by ' on rendering and not on binding
asp.NET javascipt
Jamil
I would love to change the world, but they won't give me the source code.
atconway
All-Star
16846 Points
2756 Posts
Re: How to Escape single quote in JavaScript?
Oct 19, 2011 08:38 PM|LINK
Old post, but the simplest way if using .NET Framework 4.0 or later -> use the following code:
System.Web.HttpUtility.JavaScriptStringEncode("Hello, this is John's Site")How To: Encode a JavaScript string in .NET To Escape Characters (i.e. Single Quotes) Automatically:
http://allen-conway-dotnet.blogspot.com/2011/03/how-to-encode-javascript-string-in-net.html