简体   繁体   中英

How to access a c# array from javascript in asp.net

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM