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

Discussion in 'ASP .Net' started by Mel, Mar 10, 2010.

  1. Mel

    Mel Guest

    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, Mar 10, 2010
    #1
    1. Advertising

  2. Re: Calling Javascript from Server Side - Error When Passing Data Contains Single Quote

    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
     
    Andrew Morton, Mar 11, 2010
    #2
    1. Advertising

  3. Mel

    Mel Guest

    On Mar 11, 3:00 am, "Andrew Morton" <>
    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.
     
    Mel, Mar 11, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    13,423
  2. Eric Layman
    Replies:
    3
    Views:
    664
    Rad [Visual C# MVP]
    Apr 14, 2007
  3. Evan
    Replies:
    1
    Views:
    419
    Marc 'BlackJack' Rintsch
    Nov 4, 2008
  4. Evan
    Replies:
    3
    Views:
    552
    Marc 'BlackJack' Rintsch
    Nov 4, 2008
  5. vikrant
    Replies:
    8
    Views:
    393
    vikrant
    May 17, 2007
Loading...

Share This Page