How to pass array from server side to Javascript function on client side

How do I pass an array I have created on the server side onto the client side for manipulation by Javascript?

Any pseudo code will help


You'll need to embed it as a javascript array declaration into the page. There are a number of ways to do this, but it generally means turning the array into text that you write to the page, probably using the ClientScriptManager.

I'm hoping for better javascript integration in a upcoming verison of ASP.Net. Moving the value of a server variable —any server variable— to the client ought to be supported with a simple, one-line function call. Not the back-flips we need right now.

Another way would be to use the RegisterArrayDeclaration method of the Page object (deprecated) or in the ClientScriptManager class. See MSDN for details.

The way I do it is like this:


    private string buttonarray =  "'but1','but2','but3','but4'";

    public string Buttonarray
        get { return buttonarray; }


var buttonarray = new Array(<%=Buttonarray%>);

I hope this helps.

Convert it to string representation of a javascript array ("['val1','val2','val3']") and shove it into the value field of a hidden input.

Easiest way is to convert it to json. Then just put it at the top of the page in a variable. I've found this the best json implementation for .net:

Need Your Help

How do I insert Chinese characters into a SQLExpress text field?

character-encoding full-text-search sql-server-express cjk

How do I insert Chinese characters into a SQLExpress text field? I'm using SQL Express from VS 2008. When I add Chinese characters, either via an import app I wrote or by pasting them in from the d...