I would like to call a javascript function from an aspx control. For instance, suppose I had:
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;
}