如何在for循环代码中调用javascript?

时间:2011-09-05 06:53:42

标签: c# javascript ajax

protected void Button1_Click(object sender, EventArgs e)
{
    for (int i = 0; i < 100; i++)
    {
        Page.ClientScript.RegisterClientScriptBlock(GetType(), "myScript", "<script>alert('hello world');</script>");
    }
}

警报只执行一次,是否可以在迭代中每次都执行警报?

2 个答案:

答案 0 :(得分:7)

是的,将其更改为,注意“myScript”+ i,它会在每次迭代时更改密钥:

for (int i = 0; i < 100; i++)
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "myScript" + i, "<script>alert('hello world');</script>");
        }

答案 1 :(得分:2)

我已经得到了解决方案,因此你必须在每个循环中更改密钥 像:

Page.ClientScript.RegisterClientScriptBlock(GetType(), "myScript", "<script>alert('hello world');</script>");
        Page.ClientScript.RegisterClientScriptBlock(GetType(), "myScript1", "<script>alert('hello world');</script>");