我有两个带有onclick
事件处理程序的按钮,它们处理来自grid views
页面上两个的信息。每个 gv 一个按钮。它们位于彼此之上,嵌套在html
表结构中。
这些按钮用于将grid
日期导出到Excel
文档中(参见下面的代码)
顶部按钮和grid
当click
按钮工作正常但底部按钮抛出一个ThreadAbortException: Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
当然,我为此使用了 Google-d,但一些最佳结果是处理Response.Redirect()
调用而不是Response.End()
. Forums.asp.net上的一篇这样的帖子在同一个方法调用上存在相同的错误,但解决方案是使用错误页面作为参数更改代码Response.Redirect()
- 再次与我所拥有的无关。
Microsoft 支持页面上的另一个搜索建议了一个解决方案,其中HttpContext.Current.ApplicationInstance.CompleteRequest()
替换Response.End()
. 我已经尝试过了,错误消失了,Excel 下载弹出窗口也消失了。
所以我不知道从这里去哪里。奇怪的是,相同的代码(更少的gridview
id)适用于一个但另一个。这是供您查看的代码,我已经标记了引发错误的位置。我想也许我可以产生一个新线程 - 这会缓解这个问题吗?我从未做过multi-threaded
应用程序,但我准备迎接挑战。
<table>
<tr>
<td align="left">
<asp:Button ID="btnExport" runat="server" OnClick="btnExport_Click"
Text="Export" Visible="false" />
</td>
</tr>
<tr>
<td>
<asp:Panel runat="server" ID="pnl1" Visible="false">
<asp:GridView ID="gvCountTotalsCat" runat="server"
AutoGenerateColumns="false"
CellPadding="3" PageSize="25" BackColor="White" BorderColor="MidnightBlue"
BorderStyle="Groove" BorderWidth="1px" CssClass="TextCompact"
GridLines="Vertical"
OnRowDataBound="gridView_OnRowDataBound"
EmptyDataText="Your request has returned zero records">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server" ID="lblHeader" Text="Cat" />
</HeaderTemplate>
<ItemTemplate>
<asp:Literal ID="litWuc" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Cat Entries" HeaderText="Cat Entries" />
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server" ID="lblHeader" />
</HeaderTemplate>
<ItemTemplate>
<asp:Literal ID="litSum" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</td>
</tr>
<tr>
<td align="left">
<asp:Button ID="btnExport1" runat="server" OnClick="btnExport_Click1"
Text="Export" Visible="false" />
</td>
</tr>
<tr>
<td>
<asp:Panel runat="server" ID="pnl2" Visible="false">
<asp:GridView ID="gvCountTotalsCat1" runat="server"
AutoGenerateColumns="false"
AllowPaging="false" CellPadding="3" PageSize="25" BackColor="White"
BorderColor="MidnightBlue" BorderStyle="Groove" BorderWidth="1px"
CssClass="TextCompact" GridLines="Vertical"
OnRowDataBound="gridView_OnRowDataBound"
EmptyDataText="Your request has returned zero records">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server" ID="lblHeaderWuc" Text="Wuc" />
</HeaderTemplate>
<ItemTemplate>
<asp:Literal ID="litWuc" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Wuc Entries" HeaderText="Wuc Entries" />
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server" ID="lblHeader" />
</HeaderTemplate>
<ItemTemplate>
<asp:Literal ID="litSum" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</td>
</tr>
</table>
public void btnExport_Click(object sender, System.EventArgs e)
{
string attachment = string.Empty;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
frm.Attributes["runat"] = "server";
attachment = "attachment; filename=gvCountTotalsCat_" + _selectedSite + ".xls";
gvCountTotalsCat.Parent.Controls.Add(frm);
frm.Controls.Add(gvCountTotalsCat);
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
frm.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public void btnExport_Click1(object sender, System.EventArgs e)
{
string attachment = string.Empty;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Response.Clear();
Response.ClearHeaders();
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
frm.Attributes["runat"] = "server";
attachment = "attachment; filename=gvCountTotalsCat1_" + _selectedSite + ".xls";
gvCountTotalsCat1.Parent.Controls.Add(frm);
frm.Controls.Add(gvCountTotalsCat1);
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
frm.RenderControl(htw);
Response.Write(sw.ToString());
try
{
>> Error thrown here >> Response.End();
}
catch (System.Threading.ThreadAbortException lException)
{
lException;
}
}