我有一个名为company
的下拉列表,一个名为export to excel
的按钮和一个网格视图,我还有一个更新面板用于这两个控件,但是当我导出到excel时,下拉菜单将不再起作用,即使我想导出2次,除非刷新页面。
我的出口代码:
protected void Button1_Click(object sender, EventArgs e)
{
// HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", "Kontratat-" + DateTime.Now.ToShortDateString() + ".xls"));
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
hw.WriteLine("<center><b><u><font size='5'>Kontratat</font></u></b></center>");//will be displayed in excel as a heading.
GridView1.Parent.Controls.Add(frm);
frm.Controls.Add(GridView1);
frm.RenderControl(hw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
和我的aspx代码
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" CellSpacing="2" ForeColor="Black">
</asp:GridView>
</ContentTemplate>
请帮帮我
发布于 2016-07-10 00:30:18
我认为在应该呈现更新面板内容的代码中返回XLS文件不是一个好的解决方案。为什么不重定向到一个包含按钮点击代码的新页面作为page_load呢?这样,一个完整的新页面将生成该文件,浏览器将要求保存该文件,然后它将自动重定向到您的主要页面。你能试试那个方法吗?
https://stackoverflow.com/questions/38283673
复制相似问题