473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Access Array of C# from Javascript

85 64KB
Hi:
i want to access the values of an array declared in Code behind through javascript, but i dont have any idea how to do it.. below is my code of array in C#
Expand|Select|Wrap|Line Numbers
  1.  if (dt.Rows.Count > 0)
  2.             {
  3.                 int t = Convert.ToInt32(dt.Rows.Count);
  4.                 int Total=0;
  5.                string[] UserIds = new string[t];
  6.                 foreach (DataRow dRow in dt.Rows)
  7.                 {
  8.  
  9.                     UserIds[Total] = dRow["UserID"].ToString();
  10.                     Total= Total+ 1;      
  11.                 }
  12.             }
  13.  
  14.  
in above code, i get the userIds from the database and put it in the array string[] UserIds, now what i want to do is to access this array in javascript code, but dont know how to do it ,someone please help me out its urgent. Sample codes will be greatly appreciated..
Regards:
Mudassir
Jul 10 '12 #1

✓ answered by Mudassir

well i found the solution myself, the solution is as follows as it might help some one
Expand|Select|Wrap|Line Numbers
  1. public string GetJSon()
  2. {
  3.  DataTable dt= //Call the function to fill dt"
  4.  if (dt.Rows.Count > 0)
  5.  {
  6.        int t = Convert.ToInt32(dt.Rows.Count);
  7.        int Total=0;
  8.        string[] UserIds = new string[t];
  9.        foreach (DataRow dRow in dt.Rows)
  10.        {
  11.          UserIds[Total] = dRow["UserID"].ToString();
  12.          Total= Total+ 1;      
  13.        }
  14.   }
  15. return (new JavaScriptSerializer()).Serialize(UserIds);//it will serialize the array in the javascript function.. 
  16. }
  17.  
and in the javascript function call it as follow..
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function DisplayArrays(){
  3. var UserIDs =('<%= GetJSon() %>');
  4. alert(UserIDs);
  5. }
  6. </Script>
  7.  
  8.  
Regards:
Mudassir

1 11618
Mudassir
85 64KB
well i found the solution myself, the solution is as follows as it might help some one
Expand|Select|Wrap|Line Numbers
  1. public string GetJSon()
  2. {
  3.  DataTable dt= //Call the function to fill dt"
  4.  if (dt.Rows.Count > 0)
  5.  {
  6.        int t = Convert.ToInt32(dt.Rows.Count);
  7.        int Total=0;
  8.        string[] UserIds = new string[t];
  9.        foreach (DataRow dRow in dt.Rows)
  10.        {
  11.          UserIds[Total] = dRow["UserID"].ToString();
  12.          Total= Total+ 1;      
  13.        }
  14.   }
  15. return (new JavaScriptSerializer()).Serialize(UserIds);//it will serialize the array in the javascript function.. 
  16. }
  17.  
and in the javascript function call it as follow..
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function DisplayArrays(){
  3. var UserIDs =('<%= GetJSon() %>');
  4. alert(UserIDs);
  5. }
  6. </Script>
  7.  
  8.  
Regards:
Mudassir
Jul 13 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it...
1
by: Don | last post by:
Hi, I jave a form that creates checkboxes dependent on what is selected from a drop-dwon list; this is done using PHP. I have a button that allows a user to "SELECT ALL" which will calls a...
11
by: Rob | last post by:
I know, I know, don't use frames. Well, I'm stuck with these frames and I'm trying to add functionality without a complete redsign. You can look at this as a nostalgic journey. Anyway, I've got...
0
by: rajabide4u | last post by:
How to insert data into ms access using javascript
3
by: nicky77 | last post by:
Hi there, newish to javascript so grateful for any help. I am creating a test generator and have an array of textfields ("answer") created by a simple piece of PHP code. As you can see, I have...
2
by: gellis72 | last post by:
I'm working on a program that imports a bunch of data from a folder full of Excel files and compiles it into an Access DB. The Excel files have a varying number of rows and columns that need to be...
2
by: franklin moses | last post by:
how do i insert textbox values to ms access using javascript
7
by: sosamv | last post by:
Hi all!, I'm creating a app with PHP and MySQL, the system administrator is capable of creating profiles, on each profile we create, we add a custom access menu (a javascript tree view menu), theres...
1
by: Mikhail Teterin | last post by:
Hello! A major application we are using allows extending itself with custom JavaScript. They embed Rhino implementation of JS (written in Java). There is no apparent built-in way to access...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.