问题
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)};
}
回答1:
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.
回答2:
try to use this.RegisterStartupScript instead of ScriptManager.RegisterStartupScript
回答3:
Your client side do_progress
function appears to have an error in the jQuery selectors - you're currently looking for elements named progress_status
and progress_bar
when I suspect you intend to be looking for classes or IDs. If your selector doesn't match anything it doesn't raise any kind of error, it just doesn't do anything.
来源:https://stackoverflow.com/questions/1920073/client-method-called-by-scriptmanager-registerstartupscript-not-firing