I would like to call a javascript function from an aspx control. For instance, suppose I had:
If you are interested in processing Javascript on the server, there is a new open source library called Jint that allows you to execute server side Javascript. Basically it is a Javascript interpreter written in C#. I have been testing it and so far it looks quite promising.
Here's the description from the site:
Differences with other script engines:
Jint is different as it doesn't use CodeDomProvider technique which is using compilation under the hood and thus leads to memory leaks as the compiled assemblies can't be unloaded. Moreover, using this technique prevents using dynamically types variables the way JavaScript does, allowing more flexibility in your scripts. On the opposite, Jint embeds it's own parsing logic, and really interprets the scripts. Jint uses the famous ANTLR (http://www.antlr.org) library for this purpose. As it uses Javascript as its language you don't have to learn a new language, it has proven to be very powerful for scripting purposes, and you can use several text editors for syntax checking.
You can use the Page.ClientScript.RegisterStartupScript method.
I think you want to execute the javascript serverside and not in the browser after post-back, right?
That's not possible as far as I know
If you just want to get it execute after postback, you can do something like this:
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "xx", "<script>test("+x+","+y+");</script>");
Response.Write("<scrip" + "t>test(" + x + "," + y + ");</script>");
breaking up the script keyword because VStudio / asp.net compiler doesn't like it
include script manager
code behind function
ScriptManager.RegisterStartupScript(this, this.GetType(), "HideConfirmBox", "javascript:HideAAConfirmBox(); ", true);
<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;
}