locked
building a string in VB to use in javascript - quotes issue RRS feed

  • Question

  • User987320842 posted

    Hi,  I am building a string in a vb and adding it to a dataset, with this new value i then use it in a repeater.  There is no problem with this, the string below is correct and works fine.  However I now need to use the same value to populate a javascript array.  This has raised an issue as this string will be surrounded by quotes, and therefore can not use " in the string.  I have tried to use javascript escape characters in vb whilst building the string, however this does not work. Does any one have any suggestions?

     String: 

    row.Item("BANKSTYLE") = "<a href=""#"" onclick=""javascript:someFunction('sourceFile=apage.aspx|qs=&itemId=" & checkNull.checkNullNum(row.Item("ID")) & "&cellId=style" & checkNull.checkNullNum(row.Item("ID")) & "&currVal=1|targetItem=popWindow');return false;"" title=""click to change style"">style</a>"
     To be used in a javascript array: 
    1    Function createArray()
    2    
    3            Dim drawStr As String = ""
    4    
    5            drawStr += "<script language=""javascript"">" & vbCrLf
    6            drawStr += "<!--" & vbCrLf
    7            drawStr += "var myArray= ["
    8    
    9            For Each row In useDS.Tables(0).Rows
    10               drawStr += "[""" & row.Item("BANKSTYLE") & """,""" other stuff """],"
    11           Next
    12   
    13           drawStr += "];" & vbCrLf
    
     
    Thursday, September 18, 2008 6:28 AM

Answers

  • User-990694832 posted

    First, I would suggest that system.text.stringbuilder is used to build the string.

    next, use chrw(unicode quotes code) for the double quotes.

    OR

    Use chr(34)

    stringbuilder.append("  .... ")

    stringbuilder.append(chr(34)) your opening quotes

    stringbuilder.append(" .....")   your string inbetween quotes

    stringbuilder.append(chr(34)) your closing quotes

    Hope this helps

    Also, this is not an issue in vs2008

    Hope this helps

    DK 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 18, 2008 8:23 AM

All replies

  • User-990694832 posted

    First, I would suggest that system.text.stringbuilder is used to build the string.

    next, use chrw(unicode quotes code) for the double quotes.

    OR

    Use chr(34)

    stringbuilder.append("  .... ")

    stringbuilder.append(chr(34)) your opening quotes

    stringbuilder.append(" .....")   your string inbetween quotes

    stringbuilder.append(chr(34)) your closing quotes

    Hope this helps

    Also, this is not an issue in vs2008

    Hope this helps

    DK 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 18, 2008 8:23 AM
  • User987320842 posted

    Thanks for the reply.  Sorry had a mental block, but looking at your code made it a little clearer.  I still used a normal string, but built it a little different, adding the escape characters in a seperatly,

    i.e.  

    row.Item("BANKSTYLE") = "<a href='#' onclick=" & "\""" & "javascript:someFunction('sourceFile=apage.aspx|qs=&itemId=" & checkNull.checkNullNum(row.Item("ID")) & "&cellId=style" & checkNull.checkNullNum(row.Item("ID")) & "&currVal=1|targetItem=popWindow');return false;" & "\""" & " title='click to change style'>style</a>"
     
    Thursday, September 18, 2008 10:38 AM