Add C# arraylist to JavaScript arraylist RRS feed

  • Question

  • I am looking for a way to pass variables from C# code behind to a JavaScript arraylist.

    Doing something like, define an arraylist,
    ArrayList listname = new ArrayList;

    then applying or passing this to a JavaScript arraylist.
    var customarray=new Array('one', 'two', 'three'');

    Which can be then used in a function like the one below.
    var obj = actb(document.getElementById('tb'),customarray);
    Tuesday, February 27, 2007 2:48 PM

All replies

  • you can convert it manualy like this

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < listname.Count; i++)


        sb.AppendFormat("'{0}', ", listnameIdea);



    //remove last two chars

    sb.Remove(sb.Length - 2, 2);


    string elements = sb.ToString();

    and then to use result

    var customarray=new Array(<%= elements %>);

    hope this helps

    Tuesday, February 27, 2007 5:13 PM
  • In 2.0 version, you can use ClientScriptManager.RegisterClientScriptBlock to add client-side scripts. Here's what I copied from MSDN help. The benifit of using this method is to keep such code in the code-behind file

    // Get a ClientScriptManager reference from the Page class.


    ClientScriptManager cs = Page.ClientScript;

    Type cstype = this.GetType();


        // Check to see if the client script is already registered.
        if (!cs.IsClientScriptBlockRegistered(cstype, "MyBlock"))
          StringBuilder cstext2 = new StringBuilder();
          cstext2.Append("<script type=text/javascript> function DoClick() {");
          cstext2.Append("Form1.Message.value='Text from client script.'} </");
          cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);

    Tuesday, February 27, 2007 7:09 PM