lelewin
lelewin

Reputation: 559

How can I call JavaScript function in a loop

I would like to call JavaScript function many time . I have tried like that
In Script

 function changeIt(strgr , idx) {
    //SomeCode
    return;  
 }    

In C#

 protected void btn_Click(object sender, EventArgs e)  
 {  
      string strgr = 001;
      for(int i=0; i<3; i++)  
      {  
          base.RunScriptBottom("changeIt(" + strgr + "," + i + ");");  
      }
 }  

But it call the script function only one time. What could I do.
With regards

Upvotes: 0

Views: 1558

Answers (2)

Charlie
Charlie

Reputation: 1332

You should add a client event to do this

<asp:Button Text="text" runat="server" OnClientClick="loop()" />
<script type="text/javascript">
    function loop() {
        var strgr = 001;
        for(var i=0; i<3; i++)  
        {  
            changeIt(strgr + "," + i );  
        }
    }
</script>

Upvotes: 0

hkutluay
hkutluay

Reputation: 6944

Check ClientScriptManager.RegisterStartupScript

By the way, you can write a js function to loop and call from server side once..

 function changeAll(strgr , from, to) {

   for(int i = from, i< to; i++)
      changeIt(strgr ,i);
 } 

Server Side:

protected void btn_Click(object sender, EventArgs e)  
 {  
      string strgr = 001;

          base.RunScriptBottom("changeAll(" + strgr + ",0,3);");  

 } 

Upvotes: 1

Related Questions