I’m sending a javascript function a string from behind code in .net. The problem is that the string is over 200 characters long and the javascript function doesn’t seem to accept the string. Is there a limit to how long the variable can be, and is there a way around this??
If dr("ReturnType").ToString() = "Override" Then
Dim teststring As String = "openWin('" & dr("ReturnValue").ToString() & "');"
ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "openWin", teststring, True)
End If
Yes, there is a limit: around 2GB, it’s what you can store in a string in .NET. But since we can safely assume that you are not sending this kind of volume from the server to the client (or at least we can hope), the other possibility is that your string is not encoded. So encode it before sending it to the client. And the best way to ensure that a string or an object is properly encoded is to JSON serialize it. In .NET this could be easily achieved with the JavaScriptSerializer class: