2

Currently I have a Javascript function that uses I can hard code values in like -

data: [1,4,7,9]

However I wish to pass in an integer list to set the values from the code behind something like -

C# Code Behind

public List<int> listOfInts = new List<int>();

protected void Button1_Click(object sender, EventArgs e)
    {
        listOfInts.Add(1);
        listOfInts.Add(4);
        listOfInts.Add(7);
        listOfInts.Add(9);

        ScriptManager.RegisterStartupScript(this, GetType(), "blah", "JSfunction()", true);
    }

Aspx

data: <% = listOfInts %>

However this breaks with the error -

0x800a1391 - Microsoft JScript runtime error: 'JSfunction' is undefined

If I remove the aforementioned line and do it like this in the function (not passing anything from the code behind like I need to) -

var listOfInts = new Array(); 
listOfInts[0] = 1;
listOfInts[1] = 2; 
listOfInts[2] = 3; 
listOfInts[3] = 4;

and then set -

data: [listOfInts[0],listOfInts[1],listOfInts[2],listOfInts[3]]

This works fine. How can I pass the values from the code behind to populate the values in the Javascript function?

2
  • 3
    Try converting it to JSON instead of just sending javascript your .Net object. Commented Oct 25, 2013 at 15:28
  • I didn't have time to throw the code together earlier... but take a look at my answer below the accepted answer. Commented Oct 25, 2013 at 16:27

2 Answers 2

3

You need to format listOfInts as a javascript array. Try adding a property in your code-behind like this:

protected string IntsAsJSArray
{   
    get 
    {
        return string.Format("[{0}]", string.Join(",", listOfInts));
    }
}

Then in your ASPX page

data: <%= IntsAsJSArray %>
Sign up to request clarification or add additional context in comments.

Comments

2

A more generic method to do this... and significantly better in my opinion would be to write something that works for any object you needed to do this for. Consider the following extension methods...

    public static T FromJson<T>(this string jsonData, Encoding encoding = null) 
        where T : class
    {
        encoding = encoding ?? Encoding.Default;
        var deserializer = new DataContractJsonSerializer(typeof(T));
        var buffer = encoding.GetBytes(jsonData);
        using (var stream = new MemoryStream(buffer))
        {
            return deserializer.ReadObject(stream) as T;
        }
    }

    public static string ToJson<T>(this T obj, Encoding encoding = null) 
        where T : class
    {
        encoding = encoding ?? Encoding.Default;
        var serializer = new DataContractJsonSerializer(typeof(T));
        using (var stream = new MemoryStream())
        {
            serializer.WriteObject(stream, obj);
            return encoding.GetString(stream.ToArray());
        }
    }

Usage then looks like this in your case...

data: <% = listOfInts.ToJson() %> 

This works whether you have a List, Int[], or any other object for that matter on your asp.net side. Also don't forget to consider what encoding your JSON text is in.

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.