Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to pass a array of values from the code behind of the asp .net to the javascript array. how to do this.
Posted

Hello,

There are many ways to achieve this, I am just mentioning few which I have used

  • You can use System.Web.Script.Serialization.JavaScriptSerializer to serialize the array variable
    HTML
    <script type="text/javascript">
    var _jsArr = <%= serializer.Serialize(arrVals) %>;
    <script>
  • You can create a string snippet containing the necessary JavaScript code and then use ClientScript.RegisterStartupScript to register the client script block.
    C#
    StringBuilder sb = new StringBuilder();
    sb.Append("<script>");
    sb.Append("var _jsArr = new Array;");
    foreach(string str in arrVals) {
        sb.Append("_jsArr.push('" + str + "');");
    }
    sb.Append("</script>");
    ClientScript.RegisterStartupScript(this.GetType(), "ArrayScript", sb.ToString());

You can also have a look at this[^] or this[^] article.

Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900