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

  • SEARCH
  • Home
  • 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

Related Questions

Does anyone know how can I passing value into the javascript function in asp:LinkButton
Does anyone know how to pass a several bytes into a Binary (or varbinary)
Does anyone know how to pass html attributes like data-date to the Html.TextBox()?? It
Does anyone know if there is a way to generate different code in the
Does anyone know how to pass Content/ContentPlaceholder information from a page, up through it's
Using Classic ASP, does anyone know if it is possible (or advisable) to put
Does anyone know if it's possible to pass a hidden request parameter in with
Does anyone know how to pass multiple parameters into a Thread.Start routine? I thought
Does anyone know how to convert a char array to a LPCTSTR in c?
Does anyone know if I can pass data from flash (action script) to java

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.