6

onclickページ上の2 つの情報を処理するイベント ハンドラーを持つ 2 つのボタンがgrid viewsあります。gv ごとに 1 つのボタン。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 で調べましたが、上位の結果のいくつかはResponse.Redirect()Response.End(). Forums.asp.netでのそのような投稿の 1 つには、同じメソッド呼び出しで同じエラーがありますが、解決策はResponse.Redirect()、エラー ページをパラメーターとしてコードを変更することでした。これも、私が持っているものとは関係ありません。

Microsoft サポートページでの別の検索では、 をHttpContext.Current.ApplicationInstance.CompleteRequest()置き換えるソリューションが提案されていますResponse.End()。これを試してみましたが、エラーはなくなり、Excel ダウンロードのポップアップも消えました。

だから、ここからどこへ行けばいいのかわからない。奇妙なのは、同じコード ( gridviewID が少ない) が一方では機能するが、他方では機能することです。これがレビュー用のコードです。エラーがスローされた場所にマークを付けました。おそらく新しいスレッドを生成できると思いました-それで問題は軽減されますか? 私は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;
    }
}
4

3 に答える 3

8

グリッドとボタンの両方が 1 つUpdate Panelにあり、一番上のボタンのみが として設定されていることがわかりましたPostBackTrigger。2番目のものを追加した後<asp:PostBackTrigger ControlID="btnExport1" />、問題は解決しました。

于 2013-03-15T16:54:48.657 に答える
0

ASPxGridViewExporterを取得せずにを使用して解決できますThreadAbortException

次のコード スニペットを試してください。

try
{
    using (MemoryStream stream = new MemoryStream())
    {
        // Write the content of the xlsx to the stream
        gridViewExporter.WriteXlsx(stream, new XlsxExportOptions(TextExportMode.Text, false, false));

        if (Response == null)
        {
            return;
        }

        // Write the byte content to the output
        Response.Clear();
        Response.AppendHeader("Content-Disposition", StringMngr.SafeFormat("attachment; filename=\"{0}.xlsx\"", "xlsxFileName"));
        Response.ContentType = "Text/xlsx";
        Response.ContentEncoding = System.Text.Encoding.Unicode;

        if (stream.Length > 0)
        {
            Response.BinaryWrite(stream.ToArray());
        }

        Response.Flush();
        Response.SuppressContent = true;
    }
}
catch (Exception ex)
{
    log.Error("An error occured while downloading in xlsx format.", ex);
}

ThreadAbortException次のメッセージを表示せずにExcelファイルをダウンロードします。

コードが最適化されているか、ネイティブ フレームがコール スタックの一番上にあるため、式を評価できません。

また、Excel ファイルを開くときに次のエラー メッセージが表示されることはありません。

Excel が myFilename.xlsx に判読できないコンテンツを検出しました。このブックの内容を復元しますか? この本の発行元が信頼できる場合は、[はい] をクリックします。

それが役に立てば幸い!:)

于 2015-04-09T09:06:27.223 に答える