Pass values from C# ASP.NET array to JavaScript array

As per the Wikipedia ASP.NET

ASP.NET is an open-source server-side web application framework designed for web development to produce dynamic web pages developed by Microsoft to allow programmers to build dynamic web sites, applications and services.

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"  
  Inherits="Arraylistjavascript" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
<html xmlns="">  
<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++) {  
  <form id="form1" runat="server">  
          <asp:HiddenField ID="HiddenField1" runat="server" />  
          <input type="button" onclick="Test();"  
value="Get Value From ArrayList" />  
using System;  
using System.Data;  
using System.Configuration;  
using System.Collections;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
public partial class Arraylistjavascript : System.Web.UI.Page  
   protected void Page_Load(object sender, EventArgs e)  
       ArrayList list = new ArrayList();  
       HiddenField1.Value = ArrayListToString(ref list);  
   private string ArrayListToString(ref ArrayList _ArrayList)  
       int intCount;  
       string strFinal = "";  
       for (intCount = 0; intCount <= _ArrayList.Count - 1; intCount++)  
           if (intCount > 0)  
               strFinal += "~";  
           strFinal += _ArrayList[intCount].ToString();  
       return strFinal;  

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post