Pass C# ASP.NET array to Javascript array

Asked
Active3 hr before
Viewed126 times

6 Answers

90%

You can use System.Web.Script.Serialization.JavaScriptSerializer to serialize the array variable HTMLCopy Code <script type="text/javascript"> var _jsArr = <%= serializer.Serialize(arrVals) %>; <script>,You can create a string snippet containing the necessary JavaScript code and then use ClientScript.RegisterStartupScript to register the client script block. C#Copy Code StringBuilder sb = new StringBuilder(); sb.Append("<script>"); sb.Append("var _jsArr = new Array;"); foreach(string str in arrVals) { sb.Append("_jsArr.push('" + str + "');"); } sb.Append("</script>"); ClientScript.RegisterStartupScript(this.GetType(), "ArrayScript", sb.ToString());,This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL),Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.

<script type="text/javascript">
   var _jsArr = < %= serializer.Serialize(arrVals) % > ; <
   script >
load more v
88%

@cfarm54-In code behind, you initialize the int[] called numbers. And then in the javascript, string.Join(", ", numbers) make the "numbers" array to be a string like "1,2,3,4". After that, we use eval('[1,2,3,4]') to convert an array-like string to an array. – Cheng Chen Aug 12 '10 at 6:06 ,while in code behind:,Some consider this intrusive for C# to inject Javascript,Notes: Use StringBuilder to build the script string as it will probably be long.

dummy sample:

< %
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); % >
var jsVariable = < %= serializer.Serialize(array) % > ;
load more v
72%

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:,We'll have to declare the array at a global context,Now I want to pass numbers to the client side and use the data in the array within JavaScript . How would I do this?

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];
load more v
65%

In this article, I will show you how to pass a C# array to a javascript function in ASP.NET. The code is straight forward and self-explanatory

  
<%@ Page Language="C#" AutoEventWireup="true"  
CodeFile="Arraylistjavascript.aspx.cs"  
  Inherits="Arraylistjavascript" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
  <title>Untitled Page</title>  
  
  <script language="javascript" type="text/javascript">  
      function Test() {  
          var listString = document.getElementById('HiddenField1').value;  
          var listArray = listString.split('~');  
  
          // Now you have an array in javascript of each value  
  
          for(var i = 0; i < listArray.length; i++) {  
              alert(listArray[i]);  
          }  
  
      }  
  </script>  
  
</head>  
<body>  
  <form id="form1" runat="server">  
      <div>  
          <asp:HiddenField ID="HiddenField1" runat="server" />  
          <input type="button" onclick="Test();"  
value="Get Value From ArrayList" />  
      </div>  
  </form>  
</body>  
</html>  
load more v
75%

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:,We'll have to declare the array at a global context,Now I want to pass numbers to the client side and use the data in the array within JavaScript . How would I do this?

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];
load more v
40%

We can use RegisterArrayDeclaration() of ClientScript to pass an array from a ASP.NET page to javascript.,Passing Array to Javascript from ASP.NET page,©2021 C# Corner. All contents are copyright of their authors.

ClientScript.RegisterArrayDeclaration("myArray", "'AAA'");
ClientScript.RegisterArrayDeclaration("myArray", "'BBB'");
ClientScript.RegisterArrayDeclaration("myArray", "'CCC'");
ClientScript.RegisterArrayDeclaration("myArray", "'DDD'");
load more v

Other "undefined-undefined" queries related to "Pass C# ASP.NET array to Javascript array"