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?
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 %>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments