簡體   English   中英

在Response.End()之后執行代碼

[英]Execute codes after Response.End()

我在Response.End()之后執行代碼時遇到麻煩。

我用它來導出數據表/ ASP表到MS Excel(.xls)。

導出完成,但是我的Gridview不再加載。 我用來加載gridview的命令是在Response.End()之后編寫的。

我嘗試使用HttpContext.Current.ApplicationInstance.CompleteRequest(); 但這沒有幫助。

我也嘗試了Response.Redirect("Charges_Trs.aspx", false); 仍然沒有用..

請任何幫助..這是我的代碼

lblNotif.Text = "Selected items have been posted. Exporting XLS ... (will be saved in your desktop)";
exportToExcel(dt); //calls the function to export the datatable to excel, i passed dt=datatable to this function
timerNotif.Enabled = true;
btnSearch_Click(null, null); //called the event of a button that loads the gridview

這是函數exporttoexcel()

public void exportToExcel(DataTable dt)
    {

        Table dTable = new Table();
        TableRow dTableRow = new TableRow();

        //Column Headings
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            TableCell tCell = new TableCell();
            tCell.Text = dt.Columns[i].ToString();
            dTableRow.Cells.Add(tCell);
        }

        dTable.Rows.Add(dTableRow);

        //Rows
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dTableRow = new TableRow();
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                TableCell tCell = new TableCell();
                dTableRow.Cells.Add(tCell);
            }
            dTable.Rows.Add(dTableRow);
        }

        if (Response.IsClientConnected)
        {
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=PostedCharges" + DateTime.Now.ToString("MMddyyyy") + ".xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            dTable.RenderControl(hw);
            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
    }

您可以在調用export之后嘗試在btnSearch click事件中添加代碼(希望將Grid與一些數據綁定),而不是調用click事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM