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

Ebikeneser

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?

Lee Bailey

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

How to pass a bool to a JavaScript function from code behind?

From Dev

Pass parameter to Javascript function from ASP code behind

From Dev

How to pass comma separate string in JavaScript function from Code behind?

From Dev

How to pass comma separate string in JavaScript function from Code behind?

From Dev

How to pass custom string in JavaScript function from code behind in?

From Dev

How to pass a value from JavaScript to code behind?

From Dev

JavaScript: Not calling javascript function from the code behind

From Dev

passing a variable to javascript function from code behind

From Dev

Javascript function is not being called from code behind

From Dev

Call Code Behind Function from JavaScript (not AJAX!)

From Dev

invoke and pass data from c# code behind to Javascript

From Dev

Update Dropdown List from Code Behind Using Javascript / JQuery

From Dev

call function from code behind in javascript without postback

From Dev

Calling JavaScript function from code behind after Response.End()

From Dev

How to call JavaScript Function From ASP Code Behind?

From Dev

Pass an argument to the function in code behind onclick

From Dev

Pass Slider Values from jQuery to Code Behind

From Dev

ASP.net - What is the best way to pass a list from code behind to java script?

From Dev

pass variable from javascript to ASP.NET behind code VB.Net

From Dev

Call javascript function with value in code-behind

From Dev

javascript function not calling from code behind when i'm calling another function within same click event

From Dev

Is there a way to tell from the (C#) code-behind when I insert into a Sharepoint list from JavaScript?

From Dev

Change list style attribute from code behind

From Dev

Modifying unordered list from code behind

From Dev

Change list style attribute from code behind

From Dev

Sending an object in a function from html to code behind

From Dev

How to use datatable from code behind in jqxgrid

From Dev

Javascript method call from code behind

Related Related

  1. 1

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

  2. 2

    How to pass a bool to a JavaScript function from code behind?

  3. 3

    Pass parameter to Javascript function from ASP code behind

  4. 4

    How to pass comma separate string in JavaScript function from Code behind?

  5. 5

    How to pass comma separate string in JavaScript function from Code behind?

  6. 6

    How to pass custom string in JavaScript function from code behind in?

  7. 7

    How to pass a value from JavaScript to code behind?

  8. 8

    JavaScript: Not calling javascript function from the code behind

  9. 9

    passing a variable to javascript function from code behind

  10. 10

    Javascript function is not being called from code behind

  11. 11

    Call Code Behind Function from JavaScript (not AJAX!)

  12. 12

    invoke and pass data from c# code behind to Javascript

  13. 13

    Update Dropdown List from Code Behind Using Javascript / JQuery

  14. 14

    call function from code behind in javascript without postback

  15. 15

    Calling JavaScript function from code behind after Response.End()

  16. 16

    How to call JavaScript Function From ASP Code Behind?

  17. 17

    Pass an argument to the function in code behind onclick

  18. 18

    Pass Slider Values from jQuery to Code Behind

  19. 19

    ASP.net - What is the best way to pass a list from code behind to java script?

  20. 20

    pass variable from javascript to ASP.NET behind code VB.Net

  21. 21

    Call javascript function with value in code-behind

  22. 22

    javascript function not calling from code behind when i'm calling another function within same click event

  23. 23

    Is there a way to tell from the (C#) code-behind when I insert into a Sharepoint list from JavaScript?

  24. 24

    Change list style attribute from code behind

  25. 25

    Modifying unordered list from code behind

  26. 26

    Change list style attribute from code behind

  27. 27

    Sending an object in a function from html to code behind

  28. 28

    How to use datatable from code behind in jqxgrid

  29. 29

    Javascript method call from code behind

HotTag

Archive