ScriptManager.RegisterStartupScript调用的客户端方法未触发 [英] Client method called by ScriptManager.RegisterStartupScript not firing

查看:225
本文介绍了ScriptManager.RegisterStartupScript调用的客户端方法未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以看下面的代码,告诉我我做错了什么吗?

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

}

我正在尝试通过循环的每一次更新客户端.在客户端(在表单标签内),我有一个名为"do_progress"的函数,该函数带有两个参数:message和percent.我的意图是客户端方法在循环的每次通过时都会触发,但是什么也没有发生.

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.

更新

感谢您的帮助. Ramiz,您的代码在我的情况下不起作用,因为它收集了循环内的所有方法(和进度),然后将它们同时发送给客户端. 这不会准确显示进度(每个循环代表已完成上传文件).在服务器端循环的每个唯一完成之后,我需要访问客户端函数do_progress.

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

但是,我没有看到结果在客户端上实时更新.进度栏没有移动,并且计数器(n个文件中的n个)没有执行任何操作.但是,当我查看innerHTML时,值已更新.很奇怪.几乎就像我需要刷新页面之类的东西,但这不是必须的.

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)};
                    }   

推荐答案

您的客户端do_progress函数在jQuery选择器中似乎有错误-您正在寻找名为<当我怀疑您打算查找类或ID时,请单击c1>和progress_bar.如果您的选择器不匹配任何内容,则不会引发任何类型的错误,那么它只会不执行任何操作.

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.

这篇关于ScriptManager.RegisterStartupScript调用的客户端方法未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆