ASP.NET AJAX进度条:从代码背后更新?

jlr*_*lin 5 .net asp.net asp.net-ajax progress-bar

我在应用程序中有一个Excel电子表格的导入功能.它目前使用FileUpload控件.我上传文件,然后对该文件运行操作.我想告诉用户正在进行的操作以及完成的百分比.我想我可以获取从Excel电子表格中提取的总行数,并在将每条记录插入数据库并更新进度条时不断划分.

问题是我似乎找不到任何更新Code Behind进度条的解决方案.我尝试使用Matt Berseth的解决方案.

看起来非常好看,但是我无法看到Code Behind的工作原理.从理论上讲,我认为将值放入页面上的文本框中,并将onchange设置为JavaScript函数来设置百分比将作为测试,但即使这样也没有给我预期的结果.

有关如何做到这一点的任何建议?

Sma*_*Dev 1

您应该知道的是,您无法使用标准 FileUpload 控件检查文件上传的状态。您可以做的是将文件上传到服务器上,然后使用 ODBC 连接到该文件,并开始在数据库中异步读取和插入行(通过向页面发出 ajax 请求或使用脚本服务)。

就进度条而言,您应该简单地使用 CSS 进度条(您可以在以下位置找到一个简单的示例: http: //css-tricks.com/examples/ProgressBars/)。

然后,您应该使用可以从服务器返回进度状态的方法构建一个脚本服务(使用 Web 服务):

您的 *.asmx 文件应包含类似以下内容:

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

您的 aspx 页面应包含以下内容:

<asp:ScriptManager runat="server" ID="ScriptManager">
 <Services>
    <asp:ServiceReference Path="~/services/import.asmx" InlineScript="false" />
 </Services>
</asp:ScriptManager>
Run Code Online (Sandbox Code Playgroud)

然后,您应该能够从 JavaScript 定期调用该方法(例如,每秒使用 setTimeout)并使用简单的 JavaScript 或 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());
}
Run Code Online (Sandbox Code Playgroud)

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

我希望这有帮助!