JavaScript Pass List<int> from code behind to use in Javascript function

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?

Answer:1

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 %>
Answer:2

I want to sort my table data in expandable table view, so I tried the below code: Code Link and my view is as in the below image When I use this code and put in my site, it is not working in this ...

I want to sort my table data in expandable table view, so I tried the below code: Code Link and my view is as in the below image When I use this code and put in my site, it is not working in this ...

With PhantomJS, I want to gather perfomance timing on page load time. I test this 2 methods for gathering metrics Setting own timing var page = require('webpage').create(); var system = ...

With PhantomJS, I want to gather perfomance timing on page load time. I test this 2 methods for gathering metrics Setting own timing var page = require('webpage').create(); var system = ...

I have a jqgrid and the first column I have a checkbox (checkAll others checkbox). I don't know how i do it. How I do a function to select all others checkbox in my jqgrid? $.getJSON("/Page/Table", ...

I have a jqgrid and the first column I have a checkbox (checkAll others checkbox). I don't know how i do it. How I do a function to select all others checkbox in my jqgrid? $.getJSON("/Page/Table", ...

  1. function select all checkbox
  2. select checkbox function

i'm trying to make a popup to draggable. does anyone help me how to do that, here is my code http://jsfiddle.net/Ur5Xn/38/ $(document).ready(function(){ function showAlertBox(){ $("#alert"...

i'm trying to make a popup to draggable. does anyone help me how to do that, here is my code http://jsfiddle.net/Ur5Xn/38/ $(document).ready(function(){ function showAlertBox(){ $("#alert"...

  1. make modal popup draggable jquery
  2. make modal popup draggable