0

I have a JavaScript function which is not calling a function in codebehind more than once. I have debugged it and I can confirm that it is not calling more than once. Is it something that I am missing?

JavaScript code:

function loader() {
            for(var i=0;i<<%=array1.Length%>;i++)
            {<%increment();%>;
             alert(<%=counter%>);
            }

        }

codebehind function:

public int counter = -1;
       public void increment()
       { counter++; }
1
  • What is your actual question? Commented Apr 7, 2016 at 15:08

2 Answers 2

1

As @RonWilliams says, that code is only executed at the begining, when the aspx is rendering, and not anymore.

The only way the code is executed in the form you write it, is for example inside a asp:Repeater or inside a asp:TemplateField in a GridView.

If you want to call a server-side functions try with AJAX or with Page Methods

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

Comments

1

You can't really intertwine your client-side Javascript code and your server-side C# code like this. Basically, once the rendering has been done on your page, you aren't really going to access the server until either a PostBack has occurred or your use AJAX to call an exposed WebMethod.

It'll call it once when the page initially renders, otherwise, you would need to use one of the other techniques.

WebMethod Approach

You could accomplish this as I mentioned earlier by taking advantage of a WebMethod, which would involve you creating a method in your code-behind that looks something like this :

[WebMethod]
public static void IncrementCounter()
{
     // Since you want to return the incremented value, use ++counter
     return ++counter;
}

And then you would need to add a reference in your ASPX page to the jQuery library, which will be used to handle performing AJAX calls to access this server-side method :

<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script>
    // This will ensure that your jQuery code is ready to run
    $(function(){
          // When the page is ready, call your loading function
          loader();
    });

    function loader() {
        for(var i=0; i< <%=array1.Length%>; i++){
            // This will call your WebMethod
            $.post('YourPage.aspx/IncrementCounter', function(count){
                  // count will contain the counter value
                  alert(count);
            });
        }
    }
</script>

2 Comments

thanks for the answer, i understand that this is the issue, but why is such functionality even available, if it can't be put to use ? is there any workaround other than what you have mentioned with the existing code i have, help is really appreciated !
I've just edited my response to include a basic example of how to handle calling your server-side code via a Web Method, which hopefully helps you out.

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.