Click here to Skip to main content
Click here to Skip to main content

Pass an array from ASP.NET(server) to javascript (client side)

By , 4 Jul 2010
 
I see many blogs asking a way to pass an array from server side to java script, most of them are unanswered or suggesting a ListBox (which is not simple). But to pass an array from server side to access it from client side we can use RegisterArrayDeclaration method.
 
Here is a simple example
 
In your code behind you can use the method like,
protected void Page_Load(object sender, EventArgs e)
{
    RegisterArrayDeclaration("MyArray", "'Welcome'");
    RegisterArrayDeclaration("MyArray", "'Hai'");
    RegisterArrayDeclaration("MyArray", "'Hello'");
    RegisterArrayDeclaration("MyArray", "'Best'");
    RegisterArrayDeclaration("MyArray", "'Super'");
}
You can access this array from client side Javascript code,
for (var i = 0; i < MyArray.length; i++) {
       alert(MyArray[i]);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Viswanath V
Software Developer (Senior) Concord IT Services
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 5 Jul 2010
Article Copyright 2010 by Viswanath V
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid