我刚开始学习如何使用 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);
}
}
}
}