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"  
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>  
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();  
       list.Add("test1");  
       list.Add("test2");  
       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;  
   }  
  
}

1 تعليقات

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

  1. Thanks for sharing.
    Brand Animators - Bringing Your Vision to Life with Innovative Video Solutions. From start-ups to large corporations, every business has a unique story to tell. At Brand Animators, our mission is to help you tell that story in a captivating and memorable way. As a top quality 3d, 2d animation video production company in Houston, Texas and best explainer video company in San Antonio, Texas and professional Healthcare & Medical animation video making studio in Dallas, Texas offer services such as 2d animation videos 3d animation videos, Motion graphics, 3d medical animation, Healthcare videos, Corporate films, Whiteboard animation videos, 3d Architectural walkthrough animation, Marketing and training videos, Product videos. And more! Whether you're looking to promote your brand, educate your audience, or simply convey information in an engaging and visually appealing manner, Brand Animators has you covered. Our video solutions are tailored to your budget, timeline, and company objectives.

    ردحذف
أحدث أقدم

Blog ads

CodeGuru