1

how can i access an array, defined in the form, inside javascript. e.g i have defined an integer array inside the web form like

<%  using (Html.BeginForm())
   {
       int[] ctid = ViewData["ct"] as int[];
       var x = 0;    
       %>

now i want to access this array(ctid) inside javascript, how can i do that...

1 Answer 1

2

You'll need to loop through the array and write it out to a Javascript array.

%>
//page JavaScript
var js_ctid = new Array(<% =ctid.length %>);

<%

//C# code
for (int=0; i<ctid.length; i++)
{
    Response.Write("js_ctid[" + i + "] = " + js_ctid[i]);
} 

You could also try the JavaScriptSerializer Class (http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx) to turn it into a JSON object

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.