EXCEL in AJAX cannot be exported in the original method, because Response in asynchronous mode. write errors occur. When loading a page, you can register the export button as a synchronization method. At the same time, you cannot register the export button in UpdatePanel to solve this problem. [This solution can be solved with ajax provided by VS. After finding it online for a long time, many of them use third-party ajax (not provided by vs) to export excel. It is hard to find this solution]
C # code:
Protected void Page_Load (object sender, EventArgs e)
{
PostBackTrigger trigger = new PostBackTrigger ();
Trigger. ControlID = "btn_export ";
(UpdatePanel) Page. Controls [0]. FindControl ("UpdatePanel1"). Triggers. Add (trigger );
}
// Reload the VerifyRenderingInServerForm method. Otherwise, the following error message is displayed during running: the "GridView1" control of the "GridView" type must be placed in the form tag with runat = server"
Public override void VerifyRenderingInServerForm (Control control Control)
{
// Override VerifyRenderingInServerForm.
}
Front-end code:
<Asp: UpdatePanel ID = "UpdatePanel1" runat = "server">
<ContentTemplate>
<Span style = "color: #000000; font-family: ">
<Asp: Button ID = "btn_export" runat = "server" Text = "Export" CssClass = "button100" Width = "80px" OnClick = "Button6_Click"/> </span>
<Asp: GridView ID = "gv_oil" runat = "server"
Cellpadding = "4" forecolor = "#333333" gridlines = "NONE" width = "100%">
<Footerstyle backcolor = "#507cd1" font-bold = "true" forecolor = "white"/>
<Rowstyle backcolor = "# eff3fb"/>
<Pagerstyle backcolor = "# 2461bf" forecolor = "white" horizontalalign = "center"/>
<Selectedrowstyle backcolor = "# d1ddf1" font-bold = "true" forecolor = "#333333"/>
<HeaderStyle BackColor = "#507CD1" Font-Bold = "True" ForeColor = "White"/>
<EditRowStyle BackColor = "# 2461BF"/>
<AlternatingRowStyle BackColor = "White"/>
</Asp: GridView>
</ContentTemplate>
<Triggers>
</Triggers>
</ASP: updatepanel