Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem passing data to javascript

Problem passing data to javascript

Scheduled Pinned Locked Moved ASP.NET
helpjavascriptdatabasetools
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TAK78
    wrote on last edited by
    #1

    I'm retrieving data from the database and try to pass it on to a javascript function so I can process it. I get a javascript error when I try to pass the value. Here is my code: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim val As String = CType(FormView2.FindControl("ImgList"), HtmlInputHidden).Value MsgBox(val) Dim script As String = "" script = "" script &= "var str = String(" & val & ");" script &= "alert(str);" script &= "setImageArr(" & str & ");" script &= "" Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "SetList", script) End Sub When I check via MsgBox the right value shows, but it want show in the javascript. Any suggestions. Thanks

    E G 2 Replies Last reply
    0
    • T TAK78

      I'm retrieving data from the database and try to pass it on to a javascript function so I can process it. I get a javascript error when I try to pass the value. Here is my code: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim val As String = CType(FormView2.FindControl("ImgList"), HtmlInputHidden).Value MsgBox(val) Dim script As String = "" script = "" script &= "var str = String(" & val & ");" script &= "alert(str);" script &= "setImageArr(" & str & ");" script &= "" Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "SetList", script) End Sub When I check via MsgBox the right value shows, but it want show in the javascript. Any suggestions. Thanks

      E Offline
      E Offline
      Eduard Keilholz
      wrote on last edited by
      #2

      The error is right here :

      TAK78 wrote:

      script = "" script &= "var str = String(" & val & ");" script &= "alert(str);" script &= "setImageArr(" & str & ");" script &= ""

      First of all, use a stringbuilder !! StringBuilder sbScript = new StringBuilder(); sbScript.Append(""); sbScript.Append(Environment.NewLine); sbScript.Append("alert('"); sbScript.Append(str.Replace("'", "\\'")); sbScript.Append("')"); sbScript.Append(Environment.NewLine); scScript.Append(setImageArr('"); sbScript.Append(str); sbScript.Append(')"); sbScript.Append("");

      .: I love it when a plan comes together :. http://www.zonderpunt.nl

      1 Reply Last reply
      0
      • T TAK78

        I'm retrieving data from the database and try to pass it on to a javascript function so I can process it. I get a javascript error when I try to pass the value. Here is my code: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim val As String = CType(FormView2.FindControl("ImgList"), HtmlInputHidden).Value MsgBox(val) Dim script As String = "" script = "" script &= "var str = String(" & val & ");" script &= "alert(str);" script &= "setImageArr(" & str & ");" script &= "" Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "SetList", script) End Sub When I check via MsgBox the right value shows, but it want show in the javascript. Any suggestions. Thanks

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        TAK78 wrote:

        I get a javascript error when I try to pass the value.

        Standard question #2: What error message do you get?

        TAK78 wrote:

        script &= "setImageArr(" & str & ");"

        As you have no variable named str in your server code, a new empty variable is created. This code will therefore not put any parameter in the Javascript code. You should have Option Explicit in your code to protect you from inadvertently create variables. You have to put quotes or apostrophes around the string value in the Javascript code. Also you have to encode the value properly, or you will have problems with certain characters.

        Dim script As String = _
        "<script type=""text/javascript"">" + _
        "var str='" + val.Replace("\","\\").Replace("'","\'") + "';" + _
        "alert(str);" + _
        "setImageArr(str);" + _
        "</script>"

        --- single minded; short sighted; long gone;

        T 1 Reply Last reply
        0
        • G Guffa

          TAK78 wrote:

          I get a javascript error when I try to pass the value.

          Standard question #2: What error message do you get?

          TAK78 wrote:

          script &= "setImageArr(" & str & ");"

          As you have no variable named str in your server code, a new empty variable is created. This code will therefore not put any parameter in the Javascript code. You should have Option Explicit in your code to protect you from inadvertently create variables. You have to put quotes or apostrophes around the string value in the Javascript code. Also you have to encode the value properly, or you will have problems with certain characters.

          Dim script As String = _
          "<script type=""text/javascript"">" + _
          "var str='" + val.Replace("\","\\").Replace("'","\'") + "';" + _
          "alert(str);" + _
          "setImageArr(str);" + _
          "</script>"

          --- single minded; short sighted; long gone;

          T Offline
          T Offline
          TAK78
          wrote on last edited by
          #4

          Thanks a lot works perfect now!!! So problem was that you have to encode the special characters before you pass it on, right? Thanks for the help and advice

          E G 2 Replies Last reply
          0
          • T TAK78

            Thanks a lot works perfect now!!! So problem was that you have to encode the special characters before you pass it on, right? Thanks for the help and advice

            E Offline
            E Offline
            Eduard Keilholz
            wrote on last edited by
            #5

            And still use a stringbuilder for string manipulation... is works a lot faster :->

            .: I love it when a plan comes together :. http://www.zonderpunt.nl

            V G 2 Replies Last reply
            0
            • E Eduard Keilholz

              And still use a stringbuilder for string manipulation... is works a lot faster :->

              .: I love it when a plan comes together :. http://www.zonderpunt.nl

              V Offline
              V Offline
              Vasudevan Deepak Kumar
              wrote on last edited by
              #6

              Perhaps there is some pointer here on StringBuilder Vs String Concatenations: http://www.codeproject.com/Purgatory/string.asp[^]

              Vasudevan Deepak Kumar Personal Homepage Tech Gossips

              1 Reply Last reply
              0
              • T TAK78

                Thanks a lot works perfect now!!! So problem was that you have to encode the special characters before you pass it on, right? Thanks for the help and advice

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                TAK78 wrote:

                So problem was that you have to encode the special characters before you pass it on, right?

                The main problem was that you didn't have any apostrophes around the string value.

                --- single minded; short sighted; long gone;

                1 Reply Last reply
                0
                • E Eduard Keilholz

                  And still use a stringbuilder for string manipulation... is works a lot faster :->

                  .: I love it when a plan comes together :. http://www.zonderpunt.nl

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  Eduard Keilholz wrote:

                  And still use a stringbuilder for string manipulation... is works a lot faster

                  Not if you write the code as I did, which produces a single call to String.Concat.

                  --- single minded; short sighted; long gone;

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups