0

I have tried many ways to pass a string from .net to javascript including .NET serializer, and register startup scripts.

I am looking for the simplest way to populate a javascript variable with a string generated in code behind.

This is what I am currently trying but output is ""

I am open to other suggestions.

VB.NET

Public Property ServerVariable As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
  Dim ServerVariable = "'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'"
End Sub

JAVASCRIPT

var data = "<%= ServerVariable %>";
...
List : [data]

DESIRED RESULT

List : ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
4
  • I guess, you already know the answer Commented Nov 24, 2014 at 11:55
  • @OP: why have you deleted your last question. I just wanted to post my answer. You've asked me to show you an example. Commented Nov 25, 2014 at 14:40
  • @TimSchmelter because I was being savaged by the vultures for not posting a question to their liking. If you could offer me advice on the right direction to take It would be greatly appreciated. Commented Nov 25, 2014 at 14:43
  • @Obsidian: some people have an off-day. Edit your question by providing the result of what you've tried(a compiler error). Then the close-vote("Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error") is pointless. Then undelete it. If you don't want to do that, use a collection type rather than multiple properties. You could do what you want with reflection but that is the very last resort and unnecessary in this case. Commented Nov 25, 2014 at 14:49

3 Answers 3

2

This should be a comment, sorry, btw you can access to public props in the aspx;ascx, so this should help

vb.net

Public Property ServerVariable As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
  ServerVariable = "['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']"

End Sub

JS

var data = "<%= ServerVariable %>";
Sign up to request clarification or add additional context in comments.

Comments

1

You are doing it correctly, only thing is that you need to do .split(",") in javascript to get desired result.

Like var list = console.log(data.split(","));

2 Comments

Does this mean I should remove my commas from string in vb?
No you should keep it as it is, that is the key point here, as you can see that i am splitting that with comma.
0

Missing get and set values. Passed variable ServerVariable needs to have a value applied to it.

  Private MyVar As String = ""

  Public Property ServerVariable As String
    Get
      Return MyVar
    End Get
    Set(value As String)
      MyVar = value
    End Set
  End Property

  MyVar = "['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.