Skip to content Skip to sidebar Skip to footer

How To Pass Array From Asp.net 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

Solution 1:

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.

Solution 2:

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

Solution 3:

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

Solution 4:

The way I do it is like this:

aspx:

privatestring buttonarray =  "'but1','but2','but3','but4'";

    publicstring Buttonarray
    {
        get { return buttonarray; }
    }

javascript

var buttonarray = newArray(<%=Buttonarray%>);

I hope this helps.

Solution 5:

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: http://litjson.sourceforge.net/

Post a Comment for "How To Pass Array From Asp.net Server Side To Javascript Function On Client Side"