views:

988

answers:

2

Hi.

Can somebody look at the below code and tell me what I am doing wrong.

for(i=0;i<=Request.Files.Count;i++)
{

int percentComplete = (int)Math.Ceiling((double)(i + 1) / (double)Request.Files.Count * 100);
string message = string.Format("{0} of {1} uploaded", i + 1, Request.Files.Count);

ScriptManager.RegisterStartupScript(this, this.GetType(), "progress", @"do_progress("+message+","+percentComplete+");", true);

}

I am trying to update the client with each pass of the loop. On the client (inside of the form tags) I have a function called "do_progress" that takes two parameters: message and percent. My intention is that the client-side method fires with each pass of the loop but nothing happens.

UPDATE

Thanks for your help. Ramiz, your code won't work in my case because it collects all the methods (and progress) inside the loop and then sends them to the client at the same time. This won't show progress accurately (each loop represents the completion of an uploaded file). I need to be accessing the client function, do_progress, after each unique completion of the server-side loop.

Also, the page has already loaded and the code is fired when a (upload) button is clicked.

Having said that, I am still having problems. I can confirm that I am getting the results I want with the below code by looking at 'selection source':

int percentComplete = (int)Math.Ceiling((double)(i + 1) / (double)Request.Files.Count * 100);

string message = string.Format("{0} of {1} uploaded", i + 1, Request.Files.Count);

ScriptManager.RegisterStartupScript(this, this.GetType(), "progress" + i, @"do_progress('" + message + "','" + percentComplete + "');", true);

But, I am not seeing the results update in real-time on the client. The progress bar isn't moving and the counter (n of n files) isn't doing anything. But, when I look at the innerHTML, the values have updated. Very odd. It is almost like I need to be refreshing the page or something but that should't be necessary.

The client side function I am using, which is placed in the form tags at the end of the page, looks like this:

function do_progress(message,percent)
                    {
                        try {
                            $('progress_status').innerHTML = message;
                            $('progress_bar').attr("style", percent + "px"); 
                        }catch(e){alert(e.message)};
                    }
A: 

message is a string value it should be enclosed in single quote "do_progress('"+message+"',"+percentComplete+");"

percentComplete contains integer so it doesn't require to enclose in single quote as message does.

string methods = string.empty;

for(i=0;i<=Request.Files.Count;i++)
{

int percentComplete = (int)Math.Ceiling((double)(i + 1) / (double)Request.Files.Count * 100);
string message = string.Format("{0} of {1} uploaded", i + 1, Request.Files.Count);

methods += "do_progress('"+message+"','"+percentComplete+"');"

}

Secondly, here, I'm incrementing all methods in a string variable and calling it in window.onload event just make sure the DOM is ready before we call the do_progress function.

ScriptManager.RegisterStartupScript(this, this.GetType(), "progress", @"window.onload = function() {"+ methods +"}", true);

However, this should not require here, call in window.onload as ScriptManager.RegisterStartupScript will call them when DOM gets ready.

I'm not sure what exactly issue at your end but this is my instant review.

Ramiz Uddin
Thanks, I have updated my question in response...
Code Sherpa
A: 

try to use this.RegisterStartupScript instead of ScriptManager.RegisterStartupScript

peacmaker
Thanks, I tried that but it doesn't seem to matter
Code Sherpa