Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Calling Javascript from Server Side - Error When Passing DataContains Single Quote

Reply
Thread Tools

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

 
 
Mel
Guest
Posts: n/a
 
      03-10-2010
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, VS200
 
Reply With Quote
 
 
 
 
Andrew Morton
Guest
Posts: n/a
 
      03-11-2010
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, VS200


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.JavaScriptSerializ er() as noted at
http://blog.foxxtrot.net/2009/10/jso...t-objects.html

Andrew


 
Reply With Quote
 
 
 
 
Mel
Guest
Posts: n/a
 
      03-11-2010
On Mar 11, 3:00*am, "Andrew Morton" <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, VS200

>
> 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.JavaScriptSerializ er() 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with single-quote and double-quote when using subprocess. Evan Python 3 11-04-2008 12:24 PM
problem with quote and single-quote when using "subprocess" Evan Python 1 11-04-2008 12:08 AM
How to handling string contains single quote and double quote vikrant Perl Misc 8 05-17-2007 04:37 PM
Datagrid on load; replace all double single quote to single quote to display to user Eric Layman ASP .Net 3 04-14-2007 07:16 AM
Single Quote Versus Double Quote In A href link knee-dragger@hotmail.com HTML 3 06-13-2006 12:42 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57