简体   繁体   English

JavaScript代码仅调用一次代码隐藏函数

[英]JavaScript code calling codebehind function only one time

I have a JavaScript function which is not calling a function in codebehind more than once. 我有一个JavaScript函数,该函数不会多次在代码隐藏中调用函数。 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: JavaScript代码:

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

        }

codebehind function: 代码隐藏功能:

public int counter = -1;
       public void increment()
       { counter++; }

You can't really intertwine your client-side Javascript code and your server-side C# code like this. 您不能真正像这样将客户端Javascript代码和服务器端C#代码交织在一起。 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 . 基本上,一旦在页面上完成渲染,就不会真正访问服务器,除非发生PostBack或使用AJAX调用公开的WebMethod

It'll call it once when the page initially renders, otherwise, you would need to use one of the other techniques. 页面最初呈现时,它将调用一次,否则,您将需要使用其他技术之一。

WebMethod Approach WebMethod方法

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来实现此目的,正如我之前提到的那样,这将涉及您在代码中创建一个看起来像这样的方法:

[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 : 然后,您需要在ASPX页面中向jQuery库添加一个引用,该引用将用于处理执行AJAX调用以访问此服务器端方法:

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

As @RonWilliams says, that code is only executed at the begining, when the aspx is rendering, and not anymore. 就像@RonWilliams所说的那样,该代码在渲染aspx时才开始执行,而不再执行。

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 . 以您编写的形式执行代码的唯一方法是,例如在asp:Repeater内部或GridViewasp:TemplateField内部。

If you want to call a server-side functions try with AJAX or with Page Methods 如果要调用服务器端函数,请尝试使用AJAXPage Methods

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM