1
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?

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) Commented Sep 5, 2011 at 7:00

2 Answers 2

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>");
        }
Sign up to request clarification or add additional context in comments.

8 Comments

@Wagas, what about doing it in update panel?it is not working in update panel
@DEN i beleive its the same except you .RegisterStartupScript( msdn.microsoft.com/en-us/library/…
for update panel you can use ScriptManager.RegisterStartupScript(..)
@james Khoury, i found out it only start calling the javascript function after the iteration. Is that possible to call it while it is looping through?
@DEN nope you can't achieve what you are looking for because the looping is done in code behind and it first compiled by the asp.net process which in turn generated the appropriate html and javascript and send output back to browser
|
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.