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

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)
 
A

Andrew Morton

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
 
M

Mel

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top