循环中的C#( .aspx.cs文件)调用javascript函数(在.aspx.cs文件中)有问题。下面的代码不起作用。什么都没发生。怎么了?我在堆栈溢出上读了几篇文章,但都没有用。
这是代码:
int i = 1;
while (i<10)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "key" + i.ToString(), "myFunction('" + i + "')", true);
i++;
}JavaScript函数
function myFunction(i){
document.write(i);
}但是这个代码有效,但我不感兴趣。
string something = "something";
Page.ClientScript.RegisterStartupScript(this.GetType(), "key", "myFunction('" + something + "')", true);发布于 2015-03-07 20:48:08
而不是循环,尝试如下:
ClientScriptManager cs = Page.ClientScript;
string csName = "MyScript";
Type csType = this.GetType();
for(int i = 1; i <= 10; i++)
{
string currentName = string.Format("{0}{1}", csName, i);
if (!cs.IsStartupScriptRegistered(csType, currentName))
{
string csText = string.Format("myFunction('{0}');", i);
cs.RegisterStartupScript(csType, currentName, csText, true);
}
}https://stackoverflow.com/questions/28919446
复制相似问题