使用UpdatePanel从UserControl下载文件

时间:2017-04-21 22:06:12

标签: c# asp.net user-controls

我的用户控件定义如下。

     <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <table>
                <tr>
                    <td>
                        <asp:ImageButton ID="ImageButton" runat="server" />
                    </td>
                    <td>
                        <asp:Label ID="lblHeader" runat="server" Text=""></asp:Label>
                    </td>
                </tr>
            </table>
            <asp:Button runat="server" ID="saveLayoutBtn" Text="Save" OnClick="SaveBtn_Click"
                ToolTip="Save current grid layout" />
            <asp:Button ID="Home_ExportExcel" runat="server" Text="Export To Excel" OnClick="Home_ExportExcel_Click" Visible="false" />
            <asp:PlaceHolder ID="placeHolder" runat="server"></asp:PlaceHolder>
            <asp:Label ID="notificationLbl" runat="server" Text="" Font-Bold="true"></asp:Label>
            <div id="DataGridWrapper">
<!--A Grid View tat displays Data-->
    </div>
    </ContentTemplate>
    </asp:UpdatePanel>

现在单击Home_ExportExcel时,我希望将GridDtata导出为Excel / xml文件。 我在usercontrol.ascx.cs

中有以下代码
protected void Home_ExportExcel_Click(object sender, EventArgs args)
        {
 GenerateWorkSheetWithSB(resultTbl, this.MasterPage.CurrentLibrary);
        }
public void GenerateWorkSheetWithSB(DataTable resultRequests, string libName, string advancedSearch = null)
        {
            WindowsImpersonationContext impersonationContext;
            if (BLSecurity.ImpersonateFacelessAccount(out impersonationContext))
            {
                int iWorkSheet = 1, iCol = 1, iRow = 1;
                System.Text.StringBuilder strExcelXml = new System.Text.StringBuilder();
                try
                {
                    iWorkSheet = 1;
                    iCol = 15;
                    iRow = resultRequests.Rows.Count + 1;
                    iWorkSheet++;
                    iRow++;
                    iCol++;
                }
                catch (Exception ex)
                {

                }

                //First Write the Excel Header
                strExcelXml.Append(ExcelHeader());
                // Get all the Styles
                strExcelXml.Append(ExcelStyles());
                // Worksheet options Required only one time 
                strExcelXml.Append(ExcelWorkSheetOptions());

                for (int i = 1; i < iWorkSheet; i++)
                {
                    // Create First Worksheet tag
                    strExcelXml.Append("<Worksheet ss:Name=\"WorkSheet" + i.ToString() + "\">");
                    // Then Table Tag
                    strExcelXml.Append("<Table ss:DefaultColumnWidth=\"150\" >");


                        SetupExcelHeaderWorksheet(strExcelXml, libName);
                        foreach (DataRow row in resultRequests.Rows)
                        //foreach (DataRow row in resultRequests.Rows)
                        SetupExcelDataWorksheet(strExcelXml, row, libName);
                    strExcelXml.Append("</Table>");
                    strExcelXml.Append("</Worksheet>");
                }
                // Close the Workbook tag (in Excel header you can see the Workbook tag)
                strExcelXml.Append("</Workbook>\n");

                #region "Write Into File"
                string uploadTempPath = "D:";   //ProfileBroker.GetProfileValue(AppProfileConst.UPLOAD_SERVER_TEMPPATH);
                string tmpFilePath = uploadTempPath + "\\HomeGridData.xml";
                string convertData = ConvertHTMLToExcelXML(strExcelXml.ToString());
                byte[] byteArray = Encoding.UTF8.GetBytes(convertData);
                //MemoryStream stream = new MemoryStream(byteArray);
                File.WriteAllBytes(tmpFilePath, byteArray);

                //this.Page.Response.Buffer = true;
                this.Page.Response.Clear();
                this.Page.Response.AddHeader("content-disposition", "attachment; filename=" + Path.GetFileName(tmpFilePath));
                this.Page.Response.ContentType = "application/octet-stream";
                this.Page.Response.TransmitFile(tmpFilePath);  //does not work inside WARP unless there is JS, refer to ascx file
                this.Page.Response.End();
                #endregion
                SecurityUtils.UndoImpersonation(impersonationContext);

            }
            else
            {
                Response.Write("<script>alert('File does not exist. Please contact tool support.')</script>");
            }
        }

我没有下载任何文件,而是在http响应中将网格数据作为xml格式。 如果我在.aspx页面上使用相同的代码,它工作正常,文件下载。 我在SO上找到thisthis,发现使用UpdatePanel可能是一个问题。但我不确定我在这里错过了什么。

1 个答案:

答案 0 :(得分:1)

要下载文件,您需要回复试试这个

 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <table>
            <tr>
                <td>
                    <asp:ImageButton ID="ImageButton" runat="server" />
                </td>
                <td>
                    <asp:Label ID="lblHeader" runat="server" Text=""></asp:Label>
                </td>
            </tr>
        </table>
        <asp:Button runat="server" ID="saveLayoutBtn" Text="Save" OnClick="SaveBtn_Click"
            ToolTip="Save current grid layout" />
        <asp:Button ID="Home_ExportExcel" runat="server" Text="Export To Excel" OnClick="Home_ExportExcel_Click" Visible="false" />
        <asp:PlaceHolder ID="placeHolder" runat="server"></asp:PlaceHolder>
        <asp:Label ID="notificationLbl" runat="server" Text="" Font-Bold="true"></asp:Label>
        <div id="DataGridWrapper">
<!--A Grid View tat displays Data-->
</div>
</ContentTemplate>
 <Triggers>
   <asp:PostBackTrigger ControlID="Home_ExportExcel" />
  </Triggers>
</asp:UpdatePanel>