views:

303

answers:

1

I am using UpdatePanel to trigger a button click event, which saves some 100+ files on a designated folder. I want the server to update the client about the status and count of files being saved.

protected void btnSave_Click(...){    
  var filesToSave = GetFilesToSave();
  foreach(var fileToSave in filesToSave){
    SaveProcessedFile(fileToSave);//It takes almost 30seconds to save a file
    UpdateStatusOnClient(fileToSave); //Don;t know what should be done here???????????
  }
}

I am looking for some implementation of "UpdateStatusOnClient" from where i can send a desired message to client, or update a label message asynchronously so that the client knows the progress and status of files being saved.

Thanks.

+2  A: 

It's not that easy... You can't actively send something from the server to the client. Only the client can make a request to query the status.

Now you already have a request running (the click on the button). But that will only finish once the 100 files have been saved. In theory, you could send a small bit of data by writing to the response and then flushing it to make sure that data is transmitted. I've never tried that, and I don't know any Ajax client that could deal with such a response.

What I would do is this: Save the current status in the ASP.NET session object, and then make another Ajax request on a timer to query the status every X seconds. Be aware that the request on the button is still running while you do this (it could cause problems). You may want to trigger an async action instead.

chris166