Last post Jan 29, 2013 07:44 AM by SoonDead
Nov 22, 2006 04:20 PM|Sanch|LINK
<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>
<ItemStyle HorizontalAlign="Center" />
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.
Nov 23, 2006 04:09 AM|jagdipa|LINK
The escape character is \ i.e.
alert('this is an example - hello O\'Neil');
Nov 23, 2006 01:44 PM|Sanch|LINK
<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?
Nov 24, 2006 02:36 AM|LudovicoVan|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
Nov 24, 2006 04:49 AM|jagdipa|LINK
Nov 24, 2006 05:31 AM|harshal_shravgi|LINK
Nov 24, 2006 09:42 AM|Sanch|LINK
Thank you sooooo much for all of your help. It worked. Thank you so much. :)
Nov 24, 2006 10:15 AM|LudovicoVan|LINK
so to say. We are "generating" it instead, and we need generate the proper code to begin with.
Hope this sheds more light. -LV
Aug 25, 2010 05:36 AM|jhallal|LINK
hi, check the following:
Note: make sure to replace the single quote by ' on rendering and not on binding
Oct 19, 2011 04:38 PM|atconway|LINK
Old post, but the simplest way if using .NET Framework 4.0 or later -> use the following code:
Jan 29, 2013 07:44 AM|SoonDead|LINK
There is a slight overlook in your post.
this is the unescaped \' string
As it will become:
this is the unescaped \\' string
A working solution in C# would be:
<%# DataBinder.Eval(Container.DataItem, "Name").ToString().Replace(@"\", @"\\").Replace(@"'", @"\'") %>