最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to call javascript in for loop code behind? - Stack Overflow

programmeradmin0浏览0评论
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>");
    }
}

The alert only execute once, is that possible to execute the alert everytime in the iteration?

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

The alert only execute once, is that possible to execute the alert everytime in the iteration?

Share Improve this question asked Sep 5, 2011 at 6:53 DENDEN 1,8938 gold badges28 silver badges51 bronze badges 1
  • I think the script can only be registered once, but anyhow it will always run after the page is sent to the client (the button click event is already finished being on the server side) – mslliviu Commented Sep 5, 2011 at 7:00
Add a ment  | 

2 Answers 2

Reset to default 7

yes by changing it to, note the "myScript" + i, it changes the key om every iteration:

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

I have got the solution and for that you have to change key at every loop like:

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

评论列表(0)

  1. 暂无评论