How to pass an array from an Asp.net server side to a client side Javascript function

How to pass an array that I created on the server side to the client side for manipulation using Javascript?

Any pseudo code will help

+6
javascript
source share
5 answers

You need to include it as a javascript array declaration on the page. There are several ways to do this, but usually it means turning the array into the text you write on the page, possibly using the ClientScriptManager.

I hope for better integration with javascript in the upcoming ASP.Net validation. Moving the value of a server variable — any server variable — a simple, single-line function call must be supported for the client. No need for reverse flips.

+3
source share

Another way is to use the RegisterArrayDeclaration method of the page object (deprecated) or in the ClientScriptManager class. See MSDN for more details.

+3
source share

How do i do this:

Aspx:

private string buttonarray = "'but1','but2','but3','but4'"; public string Buttonarray { get { return buttonarray; } } 

Javascript

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

Hope this helps.

+3
source share

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

+2
source share

The easiest way is to convert it to json. Then just put it at the top of the page in a variable. I found this best json implementation for .net: http://litjson.sourceforge.net/

0
source share

All Articles