Problem passing data to javascript
-
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
-
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
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
-
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
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;
-
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;
-
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
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
-
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
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
-
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
-
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