Calling Javascript from Server Side - Error When Passing DataContains Single Quote

  • Thread starter Thread starter Mel
  • Start date Start date
M

Mel

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)
 
Mel said:
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
 
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.
 
Back
Top