繁体   English   中英

ScriptManager.RegisterStartupScript调用的客户端方法未触发

[英]Client method called by ScriptManager.RegisterStartupScript not firing

有人可以看下面的代码,告诉我我在做什么错。

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。 我的意图是客户端方法在循环的每一遍都会触发,但是什么也没有发生。

更新

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

同样,页面已经加载,并且单击(上传)按钮时将触发代码。

话虽如此,我仍然有问题。 通过查看“选择源”,我可以通过以下代码确认获得所需的结果:

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时,值已更新。 很奇怪。 几乎就像我需要刷新页面一样,但这不是必须的。

我使用的客户端功能位于页面末尾的表单标签中,如下所示:

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

message是一个string值,应将其括在单引号"do_progress('"+message+"',"+percentComplete+");"

percentComplete包含integer因此它不需要像message一样用单引号引起来。

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+"');"

}

其次,在这里,我要增加一个string变量中的所有方法,并在window.onload事件中调用它,只需确保在调用do_progress函数之前DOM已准备就绪。

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

但是,这里不需要这样做,请在window.onload中调用ScriptManager.RegisterStartupScript将在DOM准备就绪时调用它们。

我不确定您到底会遇到什么问题,但这是我的即时评论。

尝试使用this.RegisterStartupScript而不是ScriptManager.RegisterStartupScript

您的客户端do_progress函数在jQuery选择器中似乎有错误-当我怀疑您打算查找类或ID时,您正在查找名为progress_statusprogress_bar 元素 如果您的选择器不匹配任何内容,则不会引发任何错误,它不会执行任何操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM