0

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

2
  • 3
    What JavaScript does this C# code actually emit to the page? C# code doesn't "call JavaScript" in the way you might be thinking. It just builds strings to emit to the page for the browser to interpret. Commented Jun 13, 2012 at 8:13
  • 1
    You can not call Javascript from C# code. C# code runs on the server, while JAvascript code runs in the client browser. You can only generate javascript code from C# code. In the generated code you can call the javascript function many times if you like. Commented Jun 13, 2012 at 8:15

2 Answers 2

1

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

 } 
Sign up to request clarification or add additional context in comments.

1 Comment

Do not forget that you may need to run javascript functions with ScriptManager.RegisterStartupScript if you have an UpdatePanel on your page.
0

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>

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.