<head>
<script type="text/javascript">
function test(x, y)
{
var cc = "";
for (var i = 0; i < x.length; i++)
{
cc += x[i];
}
cc += "\ny: " + y;
return cc;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" />
<p>
<asp:TextBox ID="TextBox1" Name="TextBox1" runat="server" AutoPostBack="True" TextMode="MultiLine"></asp:TextBox>
</p>
</form>
</body>
protected void Page_Load(object sender, EventArgs e)
{
int[] x = new int[] { 1, 2, 3, 4, 5 };
int[] y = new int[] { 1, 2, 3, 4, 5 };
string xStr = getArrayString(x); // converts {1,2,3,4,5} to [1,2,3,4,5]
string yStr = getArrayString(y);
string script = String.Format(" var y = test({0},{1}) ; ", xStr, yStr);
script += String.Format(" document.getElementById(\"TextBox1\").value = y ");
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "testFunction", script, true);
// this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "testFunction", script, true); // different result
}
private string getArrayString(int[] array)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.Length; i++)
{
sb.Append(array[i] + ",");
}
string arrayStr = string.Format("[{0}]", sb.ToString().TrimEnd(','));
return arrayStr;
}