0

I have an ASP.NET webform web application and I defined an array in codebehind and I want to access it in my page by javascript

public int[] GetSomeArray()
{
    int[] Labels;
    Labels = new int[8];
    Labels[0] = 100;
    Labels[1] = 90;
    Labels[2] = 111;
    Labels[3] = 121;
    Labels[4] = 81;
    Labels[5] = 102;
    Labels[6] = 93;
    Labels[7] = 103;

    return Labels;
}

How can I access these values in javascript code?

Sergii Zhevzhyk
  • 4,074
  • 22
  • 28
  • What you are trying to do? You can send it via `jquery ajax` though. – Rahul Singh Dec 10 '15 at 10:57
  • http://api.jquery.com/jquery.ajax/ Have a read of the documentation and then try figure it out. People on here don't like writing code for you, just helping you sort issues in code you have already written – David Watts Dec 10 '15 at 10:57
  • @user2399248 Can this Labels be an Enum? like `public enum Label { Label100 = 100, Label90 = 90, Label111 = 111, Label121 = 121, Label81 = 81, Label102 = 102, Label93 = 93, Label103 = 103 }` otherwise you have to get the data from ajax or in viewmodel – Jose Rocha Dec 10 '15 at 11:14
  • http://stackoverflow.com/a/16833679/3583859 Simply serialize it – Vijay Kumbhoje Dec 10 '15 at 12:10

1 Answers1

1

Modify function as follows

    public string GetSomeArray()
        {
            int[] Labels;
            Labels = new int[8];
            Labels[0] = 100;
            Labels[1] = 90;
            Labels[2] = 111;
            Labels[3] = 121;
            Labels[4] = 81;
            Labels[5] = 102;
            Labels[6] = 93;
            Labels[7] = 103;

            JavaScriptSerializer j = new JavaScriptSerializer();
            return j.Serialize(Labels);
}

and also on page add

<script type="text/javascript">
         var array = <%= GetSomeArray() %>;
</script>
Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20