Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1018595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:52:54+00:00 2026-05-16T10:52:54+00:00

Does anyone know how to pass a C# ASP.NET array to a JavaScript array?

  • 0

Does anyone know how to pass a C# ASP.NET array to a JavaScript array? Sample code will also be nice.

Sorry if I was vague earlier guys. The question is actually quite simple. Let’s say for simplicity that in my aspx.cs file I declare:

int [] numbers = new int[5];

Now I want to pass numbers to the client side and use the data in the array within JavaScript . How would I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:52:55+00:00Added an answer on May 16, 2026 at 10:52 am

    You can use ClientScript.RegisterStartUpScript to inject javascript into the page on Page_Load.

    Here’s a link to MSDN reference:
    http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx

    Here’s the code in Page_Load:

      List<string> tempString = new List<string>();
      tempString.Add("Hello");
      tempString.Add("World");
    
      StringBuilder sb = new StringBuilder();
      sb.Append("<script>");
      sb.Append("var testArray = new Array;");
      foreach(string str in tempString)
      {
        sb.Append("testArray.push('" + str + "');");
      }
      sb.Append("</script>");
    
      ClientScript.RegisterStartupScript(this.GetType(), "TestArrayScript", sb.ToString());
    

    Notes: Use StringBuilder to build the script string as it will probably be long.

    And here’s the Javascript that checks for the injected array “testArray” before you can work with it:

    if (testArray)
    {
      // do something with testArray
    }
    

    There’s 2 problems here:

    1. Some consider this intrusive for C# to inject Javascript

    2. We’ll have to declare the array at a global context

    If you can’t live with that, another way would be to have the C# code save the Array into View State, then have the JavaScript use PageMethods (or web services) to call back to the server to get that View State object as an array. But I think that may be overkill for something like this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 509k
  • Answers 509k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The way I would do it in this case is… May 16, 2026 at 4:40 pm
  • Editorial Team
    Editorial Team added an answer Yes, absolutely. You'll want to use the overload of View()… May 16, 2026 at 4:40 pm
  • Editorial Team
    Editorial Team added an answer you accessing document body, but your swf is deeper, so… May 16, 2026 at 4:40 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Further to my question on disabling validators using javascript - Disable ASP.NET validators with
With ASP.NET MVC 1.0, .NET 3.5 and C# you can easily pass a method
Looking to pass variables from c# to javascript to use some jquery code. Passing
I'm using jQuery to make an Ajax call using an Http Post in ASP.NET
I have a pretty simple ASP.NET MVC page and am using TinyMCE to allow
hey there - I'm trying to use asp.net mvc for some things as usual,
So I have an asp.net Web Application (Not Web Site) that I am trying
I'm using the Yahoo Uploader, part of the Yahoo UI Library, on my ASP.Net
I have an asp.net mvc calendar application (using jquery ui datepicker) and i am
I've got some inherited code that has a tendency to pass objects around as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.