From: Mel on
I have the following lines of code in the GridView.RowDataBound event
to call some Javascript code when the user clicks on a GridView row.
If any of the strings I am passing to the Javascript code contain a
single quote this Javascript error occurs: Error: Expected ')'. How
do I fix the first line of code below to allow for single quotes?

seldata = "userSelected('" + strCustName + "','" + strCustCompany +
"', '" + strAddress + "')"
e.Row.Attributes.Add("onclick", seldata)
'test the code above by setting strAddress = "123 Veteran's Blvd"

(using asp.net 2.0, vb.net, VS2008)
From: Andrew Morton on
Mel wrote:
> I have the following lines of code in the GridView.RowDataBound event
> to call some Javascript code when the user clicks on a GridView row.
> If any of the strings I am passing to the Javascript code contain a
> single quote this Javascript error occurs: Error: Expected ')'. How
> do I fix the first line of code below to allow for single quotes?
>
> seldata = "userSelected('" + strCustName + "','" + strCustCompany +
> "', '" + strAddress + "')"
> e.Row.Attributes.Add("onclick", seldata)
> 'test the code above by setting strAddress = "123 Veteran's Blvd"
>
> (using asp.net 2.0, vb.net, VS2008)

If you enclose the strings for Javascript in double quotes, you can use the
JSON encoding method from
http://www.west-wind.com/weblog/posts/114530.aspx

or (still using double quotes) you could use
System.Web.Script.Serialization.JavaScriptSerializer() as noted at
http://blog.foxxtrot.net/2009/10/json-encoding-of-net-objects.html

Andrew


From: Mel on
On Mar 11, 3:00 am, "Andrew Morton" <a...(a)in-press.co.uk.invalid>
wrote:
> Mel wrote:
> > I have the following lines of code in the GridView.RowDataBound event
> > to call some Javascript code when the user clicks on a GridView row.
> > If any of the strings I am passing to the Javascript code contain a
> > single quote this Javascript error occurs:  Error: Expected ')'.  How
> > do I fix the first line of code below to allow for single quotes?
>
> > seldata = "userSelected('" + strCustName + "','" + strCustCompany +
> > "', '" + strAddress + "')"
> > e.Row.Attributes.Add("onclick", seldata)
> > 'test the code above by setting strAddress = "123 Veteran's Blvd"
>
> > (using asp.net 2.0, vb.net, VS2008)
>
> If you enclose the strings for Javascript in double quotes, you can use the
> JSON encoding method fromhttp://www.west-wind.com/weblog/posts/114530.aspx
>
> or (still using double quotes) you could use
> System.Web.Script.Serialization.JavaScriptSerializer() as noted athttp://blog.foxxtrot.net/2009/10/json-encoding-of-net-objects.html
>
> Andrew

I looked at the first link. I think that will work if I can convert
the C code to VB.