ASP.NET AJAX进度条:从code背后更新? [英] ASP.NET AJAX Progress Bar: Update from Code Behind?

查看:149
本文介绍了ASP.NET AJAX进度条:从code背后更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个导入功能在应用程序中的Excel US preadsheet。它使用FileUpload控件的时刻。我上传的文件,然后运行在该文件的操作。欲通知该操作的用户正在做,而且完成的百分比。我想,我可以把我行从Excel的小号preadsheet提取的总数量,并不断分裂,因为我每次插入记录到数据库中,并更新进度条。

I have an Import function for an Excel spreadsheet within an application. It uses the FileUpload control at the moment. I upload the file, then run an operation on that file. I want to inform the user of the operation being done, and the percentage that is done. I figure that I can take the total number of rows I extracted from the Excel spreadsheet, and continuously divide as I insert each record into the database and update a progress bar.

现在的问题是,我似乎无法找到更新的code后面的进度条的任何解决方案。我尝试使用马特Berseth写的解决方案

The problem is that I can't seem to find any solution that updates a progress bar from the Code Behind. I tried using Matt Berseth's solution.

这是非常好看,但我不能看到把它从code工作落后。从理论上讲,我计算过,把一个值转换为文本框的页面上,并且onchange的设置为一个JavaScript函数来设置比例将作为一个测试,但即使如此,也没有给我想要的结果。

It's very nice to look at, but I can't see to get it working from the Code Behind. Theoretically, I figured that putting a value into a textbox on the page, and setting the onchange to a JavaScript function to set the percentage would work as a test, but even that didn't give me the desired results.

任何建议,这可怎么办呢?

Any suggestions as to how this can be done?

推荐答案

你应该知道的是,你不能使用标准的FileUpload控件检查文件上传的状态。你可以做的是上传服务器上的文件,然后使用ODBC连接并开始阅读和插入数据库中的异步线路(通过一个Ajax请求一个页面或通过脚本服务)。

What you should know is that you cannot check the status of a file upload using the standard FileUpload control. What you can do is to upload the file on the server and then connect to it using ODBC and start reading and inserting lines in your database asynchronous (by making a ajax request to a page or using a script service).

至于进度条的话,你只需要使用一个CSS PROGRES吧(你可以找到一个简单的例子在:的 http://css-tricks.com/examples/ProgressBars/ )。

As far as the progress bar goes, you should simply use a CSS progres bar (you can find a simple example at: http://css-tricks.com/examples/ProgressBars/).

那么,你应该建立一个脚本服务(使用Web serivice)有一种方法,可以从服务器返回的进展情况:

Then, you should build a script service (using a web serivice) with a method that can return the status of your progress from the server:

您* .asmx文件应包含这样的:

Your *.asmx file should contain something like:

[WebMethod]
public int GetStatus()
    {
        int progress = 0; // in percents
        // your server-side code here
        return progress;
    }

您aspx页面应该包含这样的:

Your aspx page should contain something like:

<asp:ScriptManager runat="server" ID="ScriptManager">
 <Services>
    <asp:ServiceReference Path="~/services/import.asmx" InlineScript="false" />
 </Services>
</asp:ScriptManager>

那么,你应该能够定期调用该方法从JavaScript(每秒例如,使用的setTimeout),并更新进度条的宽度与简单的JavaScript或jQuery的:

Then, you should be able to call that method from JavaScript periodically (each second for example, using setTimeout) and update the progress bar width with simple JavaScript or jQuery:

var tout, service;

function UpdateStatus() {
    if (tout != null)
         clearTimeout(tout);

    if (service == null)
         service = new namespace.here.serice_class_here();

    service.GetStatus(onSuccess, onFailure);    
}

function onSuccess(result) {
    // update the width of the progress with result (the integer value of the progress)
    progress_bar.style.width = percent + "%";        

    // call the method again
    tout = setTimeout("UpdateStatus()", 1000);
}

function onFailure(error) {
    alert(error.get_message());
}

您可以扩展您的onSuccess JavaScript函数当进度已完成(返回的值是100%),你可以将用户重定向到另一个页面或显示信息或按钮,根据您的需要。

You could extend your onSuccess JavaScript function and when the progress is complete (returned value is 100%) you could redirect the user to another page or display an info or a button depending on your needs.

我希望这有助于!

这篇关于ASP.NET AJAX进度条:从code背后更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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