-1

I have to read the Max value and Percent value into jquery function. I have tried reading these values into hidden variable and Session variable on page load which returns 0 always. These are my properties:

    public static int Max { get; set; }
    public static int Percent { get; set; }

Here is my Codebehind:

      [WebMethod]
      public static string Test()
      { 
          var y = 0;
          var max = 100000;
          Max = max;
          for(int i = 0; i < max; i++)
          {
              y++;
              Percent = y++; 
          }
          if (y > max)
              return "Success";
          else
            return "Fail";
      }

This is my ajax call on button click. I am trying to read the max value and percent value for each i with another function. I am not sure how to call those values into another function.

<asp:Button ID="btnGetData" runat="server" Text="Get Data" />

   $(document).ready(function () {
     $$('btnGetData').click(function () {
         function readValues();
         $.ajax({
             type: 'POST',
             url: 'Test.aspx/Test',
             data: "{}",
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             async: true,
             success: function (Result) {
                 alert(Result.d);
             },
             error: function (Result) {
                 alert('Error!');
             }

         });
     });
 });
function readValues(){
 var t = <%= Max%>;
 var p = <%= Percent%>;  //This has to be updated every few seconds until 
                             //the for loop is finished.
}
Chandu
  • 7
  • 2
  • your Max and Percent will get evaluated when the page loads and displayed. They won't get dynamically updated in the way you want for this - this is why they are zero as that is their initial value. One approach would be to move the processing on the server side into a separate thread/process, and have a separate Ajax call to report progress which the front end then calls repeatedly. – GPW Sep 04 '18 at 12:38
  • `/This has to be updated every few seconds until //the for loop is finished.` This is impossible. C# code runs on the server, JS code runs in the browser. In your AJAX "success" callback you will get a result from the C# code once it's completely finished processing everything you told it to. This code seems like some dummy / demo code. What is going to be the real use case here? Perhaps there is another way to achieve your actual goal. – ADyson Sep 04 '18 at 12:42

1 Answers1

1

I guess what you are trying to acheive is similar to this Question: Simple BackgroundWorker is not updating label on web page

Basically, your options are:

  • Re-architect your server-side code so the operation starts with the first call (and returns immediately), then the front end can call a different function/endpoint to see what the current progress is and report that (probably by using a timer once a second or something)
  • Use something to enable two-way communications (e.g. SignalR) so the server can report its progress back to the client.

Either of these is probably a bit too involved for a straightforward answer on StackOverflow. I suggest trying to put together something around these suggestions then coming back if you have problems.

GPW
  • 2,528
  • 1
  • 10
  • 22