添加AJAX面板时,导出到Excel停止工作

时间:2013-09-19 16:20:50

标签: c# asp.net ajax

我刚开始学习如何使用Ajax而且我坚持这个。

我的ASP.net网站上的按钮运行正常,但在添加Ajax面板后导出到Excel并导出到CSV停止工作。

他们在我添加面板之前工作,当我调试函数To_Excel_Click被调用时被处理但它只是不创建Excel文件。我认为Ajax中的某些东西阻止了文件的创建。

感谢您的帮助

HTML Ajax

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <contenttemplate>
<asp:Button ID="BT1" runat="server" Text="Submit" Visible="false" onclick="BT1"/>
 <asp:Button ID="BT2" runat="server" Text="Submit" Visible="false" onclick="BT2" />
 <asp:Button ID="BTMakeCVS" runat="server" Text="Submit to CVS" Visible="false" onclick="BTMakeCVS_Click" />
    <asp:Button ID="To_Excel" runat="server" Text="To Excel" onclick="To_Excel_Click" Visible="false" AutoPostBack="True"/>
    </fieldset><fieldset class="Matainancefieldset3">
<asp:GridView ID="GVData" runat="server" Visible="false">
         <RowStyle CssClass="Row" />
    <AlternatingRowStyle CssClass="AltRow" /> 
    <PagerTemplate><table><tr><td>
        <asp:PlaceHolder ID="ph" runat="server">
        </asp:PlaceHolder></td></tr></table>
    </PagerTemplate>
    </asp:GridView>
 <asp:Label ID="LBExcell" runat="server" Text="Model:" Visible="false"></asp:Label>
 </contenttemplate>
        </asp:UpdatePanel>
</asp:Content>

C#

protected void To_Excel_Click(object sender, EventArgs e)
{
        ExcelExport.Export("Summary.xls", GVData);
}

public class ExcelExport
{
    public static void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
              "content-disposition", string.Format("attachment;filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                Table table = new Table();
                table.GridLines = gv.GridLines;

                if (gv.HeaderRow != null)
                {
                    ExcelExport.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }

                foreach (GridViewRow row in gv.Rows)
                {
                    ExcelExport.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                if (gv.FooterRow != null)
                {
                    ExcelExport.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                table.RenderControl(htw);

                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

    private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];

            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current
as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current
as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current
as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current
as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current
as CheckBox).Checked ? "True" : "False"));
            }

            if (current.HasControls())
            {
                ExcelExport.PrepareControlForExport(current);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

Response PartialPostBack触发的UpdatePanel GridView无法完成所有操作。如果您只是将已经呈现的UpdatePanel w /数据呈现为HTML,以便将其保存为Excel,那么通过将其包装在UpdatePanel中,您实际上并没有获得任何收益。

我将导出到Excel按钮放在{{1}}之外,这样可以避免任何问题。