【问题标题】:Show Update Progress During Export在导出期间显示更新进度
【发布时间】:2018-12-08 11:41:58
【问题描述】:

单击“导出到 Excel”按钮后,使用以下 Java 脚本显示加载程序。

<script type="text/javascript">
    function showProgress() {
        var updateProgress = $get("<%= UpdateProgress1.ClientID %>");
        updateProgress.style.display = "block";
    }
</script>


<asp:Button ID="btntoExcel" runat="server" Text="Export to Excel"  onclick="btntoExcel_Click" OnClientClick="showProgress()" Width="115px" 
                CssClass="styleShowData" Font-Size="Smaller" />

一切正常,但加载器在将文件保存到系统后继续加载。

不知道如何停止加载器,因为在 JS 中它被称为

updateProgress.style.display = "block"

任何建议都会非常有帮助。

提前致谢。

在下面添加整个代码

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="ViewReport.aspx.vb" Inherits="ViewReport" %>

状况报告

    <script type="text/javascript">
        function showProgress() {
            var updateProgress = $get("<%= UpdateProgress1.ClientID %>");
            updateProgress.style.display = "block";
        }

        function hideProgress() {
            var updateProgress = $get("<%= UpdateProgress1.ClientID %>");
            updateProgress.style.display = "none";
        }   
    </script> 

<div runat="server" id="divPopupWindow" align="Left" style="position: relative; top: -11px; left: 0px; background-color: #C8B49B;">
    <marquee behavior="alternate"><asp:Label ID="mqHeader1" runat="server" Text="Scrolling Employee Info"></asp:Label></marquee>

    <br />
        <asp:LoginStatus ID="LoginStatus1" runat="server" />
    <br /><br />

    <asp:UpdatePanel runat="server" ID="UpdatePanel_1">
        <ContentTemplate>

        <div runat="server" id="divMyPageWidth_2" align="Left" style="position: relative; height: 10%; left: 0px; background-color: #C8B49B;">

        <asp:Button ID="btntoExcel" runat="server" Text="Export to Excel"  onclick="btntoExcel_Click" OnClientClick="showProgress()" Width="115px" 
            CssClass="styleShowData" Font-Size="Smaller" />

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <asp:Button ID="btnGotoMainPage" runat="server" Text="Goto Main Page"  
        Width="118px" CssClass="stGotoMainPage" 
        Font-Size="Smaller" />

                <br />
                <table>
                    <tr>
                        <td class="style2">
                            <asp:DropDownList ID="ddlStatusSelection" runat="server">
                                <asp:ListItem Selected="True">Approved</asp:ListItem>
                                <asp:ListItem>Pending</asp:ListItem>
                                <asp:ListItem>Rejected</asp:ListItem>
                            </asp:DropDownList>
                        </td>

                        <td class="style3">
                            <asp:DropDownList ID="ddlShowRecordsCount" runat="server">
                                <asp:ListItem Selected="True">25</asp:ListItem>
                                <asp:ListItem>50</asp:ListItem>
                                <asp:ListItem>100</asp:ListItem>
                                <asp:ListItem>200</asp:ListItem>
                                <asp:ListItem>300</asp:ListItem>
                                <asp:ListItem>400</asp:ListItem>
                                <asp:ListItem>500</asp:ListItem>
                                <asp:ListItem>All</asp:ListItem>
                            </asp:DropDownList>
                        </td>
                        <td>
                            <asp:Button ID="Button1" runat="server" Text="Show Data"  
                            CssClass="styleShowData" />
                        </td>
                    </tr>
            </table>

        <div runat="server" id="divMyPageWidth" align="Left" style="position: relative; background-color: #C8B49B; overflow: scroll">
                <asp:GridView ID="gvScreenToExcel" runat="server" AutoGenerateColumns="false" HeaderStyle-Wrap="True" Height="190px" Width="380px" 
                    BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" 
                    RowStyle-Wrap="false"  Font-Size="Small" >
                    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
                    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                    <SortedAscendingCellStyle BackColor="#FFF1D4" />
                    <SortedAscendingHeaderStyle BackColor="#B95C30" />
                    <SortedDescendingCellStyle BackColor="#F1E5CE" />
                    <SortedDescendingHeaderStyle BackColor="#93451F" />
                    <AlternatingRowStyle BackColor="#FFE7CE" />
                    <Columns>
                        <asp:BoundField DataField="Rec_Num" HeaderText="Record #" />
                        <asp:BoundField DataField="Remarks" HeaderText="Remarks" />
                    </Columns>
                </asp:GridView>
            </div>

        </div>
        </ContentTemplate>

        <Triggers>
            <asp:PostBackTrigger ControlID = "btntoExcel" />
        </Triggers>

    </asp:UpdatePanel>

    <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel_1">
    <ProgressTemplate>
        <div class="modal">
            <div class="center">
                <img alt="" src="Loader.gif" />
            </div>
            </div>
        </ProgressTemplate>
    </asp:UpdateProgress>
  </div>
</form>

【问题讨论】:

    标签: javascript asp.net updatepanel postback updateprogress


    【解决方案1】:

    关于这种情况的一些事实以及在解决方案之前提供的信息:

    如果对显示自定义进度指示器(而不是在下载期间运行的浏览器的进度指示器)有严格要求,请实施以下解决方案:

    • 通过 Sys.WebForms.PageRequestManager 订阅 UpdatePanel 的客户端事件,
    • 执行异步请求以显示进度指示器。例如,将“btntoExcel”按钮标记为 asp:AsyncPostBackTrigger。应该会出现进度指示器,
    • 单击“btntoExcel”按钮启动例程。处理按钮的 SERVER-SIDE Click 事件并将创建的 EXCEL 文件保存到内存或服务器文件夹中,
    • 处理客户端 UpdatePanel 的“endRequest”事件。进度指示器应该已经隐藏。向服务器发起真正的回发(例如,通过添加隐藏的“asp:PostBackTrigger”按钮或使用原始的“__doPostBack”函数),
    • 处理此按钮的 SERVER-SIDE Click 事件并执行真正的下载(以前创建的文件)。

    以下是使用提供的标记测试的完整工作代码:

    <script type="text/javascript">
        function showProgress() {
            //var updateProgress = $get("<%= UpdateProgress1.ClientID %>");
            //updateProgress.style.display = "block";
        }
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_initializeRequest(prm_InitializeRequest);
        prm.add_endRequest(prm_EndRequest);
        function prm_InitializeRequest(sender, args) {
            alert("Starting Update Panel Async Request... Saving File... Displaying Progress Indicator...");
        }
        function prm_EndRequest(sender, args) {
            alert("Update Panel Async Request Finished. File Saved, But Not Yet Downloaded. Progress Indicator Should Be Already Hidden. Starting Real Download...");
            var btnDoRealDownloadClientObject = $get("<%= btnDoRealDownload.ClientID %>");
            btnDoRealDownloadClientObject.click();
        }
    </script>
    
    <asp:UpdatePanel runat="server" ID="UpdatePanel_1">
        <ContentTemplate>
            ...
            <asp:Button ID="btntoExcel" runat="server" Text="Export to Excel" OnClick="btntoExcel_Click" OnClientClick="showProgress()" ... />
            <asp:Button ID="btnDoRealDownload" runat="server" OnClick="btnDoRealDownload_Click" style="display: none" ... />
            ...
        </ContentTemplate>
    
        <Triggers>
            <%--<asp:PostBackTrigger ControlID="btntoExcel" />--%>
            <asp:AsyncPostBackTrigger ControlID="btntoExcel" />
            <asp:PostBackTrigger ControlID="btnDoRealDownload" />
        </Triggers>
    
    </asp:UpdatePanel>
    
    <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel_1">
        ...
    </asp:UpdateProgress>
    
    //CS
    protected void btntoExcel_Click(object sender, EventArgs e) {
        Session["SavedFile"] = SAVE_FILE_AS_MEMORY_STREAM;
        ...
    }
    
    protected void btnDoRealDownload_Click(object sender, EventArgs e) {
        MemoryStream stream = Session["SavedFile"] as MemoryStream;
        ...
        Page.Response.BinaryWrite(stream.ToArray());
        Page.Response.End();
    }
    
    
    'VB
    Protected Sub btntoExcel_Click(ByVal sender As Object, ByVal e As EventArgs)
        Session("SavedFile") = SAVE_FILE_AS_MEMORY_STREAM
        ...
    End Sub
    
    Protected Sub btnDoRealDownload_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim stream As MemoryStream = CType(Session("SavedFile"), MemoryStream)
        ...
        Page.Response.BinaryWrite(stream.ToArray)
        Page.Response.End
    End Sub
    

    【讨论】:

      【解决方案2】:

      在你的btntoExcel_Click事件后面的代码中,在导出逻辑完成后添加以下内容。

      ScriptManager.RegisterStartupScript(Me, Page.GetType, "Script", "hideProgress();", True)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-19
        相关资源
        最近更新 更多