Ric Bevan

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;
listname.Add("one");
listname.Add("two");
listname.Add("three");

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

Which can be then used in a function like the one below.
var obj = actb(document.getElementById('tb'),customarray);


Re: Visual C# General Add C# arraylist to JavaScript arraylist

Galin Iliev

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






Re: Visual C# General Add C# arraylist to JavaScript arraylist

Friendly Dog

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.'} </");
cstext2.Append("script>");
cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
}