Click here to Skip to main content
15,884,176 members
Articles / Web Development / ASP.NET
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.44/5 (7 votes)
4 Jul 2010CPOL 25.2K   5   1
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,
C#
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,
C#
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)


Written By
Software Developer (Senior) Concord IT Services
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionFittoooooooooooooooooooooo.... Awsm. Pin
VICK21-Jul-13 23:13
professional VICK21-Jul-13 23:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.