从代码隐藏中传递JavaScript函数的参数

我想从aspx控件调用一个javascript函数。 例如,假设我有:

  Untitled Page  function test(x, y) { }    

并在后面的代码中:

 protected void Button1_Click(object sender, EventArgs e) { // do stuff (really going to a database to fill x and y) int[] x = new int[] { 1, 2, 3, 4, 5 }; int[] y = new int[] { 1, 2, 3, 4, 5 }; // call javascript function as test(x,y); } 

有办法吗?

您可以使用Page.ClientScript.RegisterStartupScript方法。

如果您正在使用ScriptManager或任何Ajax控件/异步回发,请查看ScriptManager.RegisterStartupScript方法。

编辑:

实际上,您想要的function可能是ScriptManager.RegisterClientScriptBlock

我发现的其他一些事情:

你不能直接传入一个数组,如:

 this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "xx", ""); 

因为它调用x和y的ToString()方法,返回“System.Int32 []”,显然Javascript不能使用它。 我必须将数组作为字符串传递,如“[1,2,3,4,5]”,所以我编写了一个辅助方法来进行转换。

此外,this.Page.ClientScript.RegisterStartupScript()和this.Page.ClientScript.RegisterClientScriptBlock()之间存在差异 – 前者将脚本放在页面底部,这是我需要的,以便能够访问控件(与document.getElementByID一样)。 RegisterClientScriptBlock()在呈现标记之前执行,因此如果我使用该方法,实际上会出现Javascript错误。

http://www.wrox.com/WileyCDA/Section/Manipulating-ASP-NET-Pages-and-Server-Controls-with-JavaScript.id-310803.html涵盖了两者之间的差异。

这是我提出的完整示例:

 // code behind protected void Button1_Click(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("test({0},{1})", xStr, yStr); 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; } //aspx page   Untitled Page    

包括脚本管理器

代码背后的function

 ScriptManager.RegisterStartupScript(this, this.GetType(), "HideConfirmBox", "javascript:HideAAConfirmBox(); ", true); 
 Response.Write("test(" + x + "," + y + ");"); 

分解脚本关键字因为VStudio / asp.net编译器不喜欢它

   Call java script function on Code behind   

cs代码

 protected void Page_Load(object sender, EventArgs e) { TextBox2.Attributes.Add("onkeypress", "return abc();"); } 

试试这个

我想你想在回复后执行javascript服务器端而不是浏览器,对吧?

据我所知,这是不可能的

如果你只想在回发后执行它,你可以这样做:

 this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "xx", ""); 

如果您对在服务器上处理Javascript感兴趣,可以使用一个名为Jint的新开源库,它允许您执行服务器端Javascript。 基本上它是用C#编写的Javascript解释器。 我一直在测试它,到目前为止看起来非常有希望。

这是网站上的描述:

与其他脚本引擎的差异:

Jint是不同的,因为它不使用CodeDomProvider技术,它使用引擎盖下的编译因此导致内存泄漏,因为编译的程序集无法卸载。 此外,使用此技术可防止像JavaScript一样使用动态类型变量,从而在脚本中提供更大的灵活性。 相反,Jint嵌入了它自己的解析逻辑,并且真正解释了脚本。 为此,Jint使用着名的ANTLR( http://www.antlr.org )库。 由于它使用Javascript作为其语言,您不必学习新语言,它已被certificate对于脚本编写非常强大,您可以使用多个文本编辑器进行语法检查。

      

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; }