导出到CSV不能在一个按钮上工作 [英] Export to CSV not working on one button

查看:84
本文介绍了导出到CSV不能在一个按钮上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个导出为CSV:



I have two Export to CSV:

<td align="right" width="50px">
                                    <asp:LinkButton ID="lnkExportCSV" runat="server"  OnClick="lnkExportCSV_Click"> <table border="0" cellpadding="0" cellspacing="0" width="40px" ><tr align="center"><td><img alt="Export to CSV" src="../Images/Export34.gif" width="32" height="32" /></td></tr><tr align="center"><td>CSV </td></tr></table></asp:LinkButton>
                                </td>
                                <td align="right" width="100px">
                                    <asp:LinkButton ID="lnkExportCSVWO" runat="server"  OnClick="lnkExportWOCSV_Click"> <table border="0" cellpadding="0" cellspacing="0" width="100px" ><tr align="center"><td><img alt="Export to CSV" src="../Images/Export34.gif" width="32" height="32" /></td></tr><tr align="center"><td>CSV for Work Order</td></tr></table></asp:LinkButton>
                                </td>





以下是他们的代码:



And here are their codes:

protected void lnkExportCSV_Click(object sender, EventArgs e)
    {
        var dateRange = string.Format("{0:MMdd}", Convert.ToDateTime(Session["DateFrom"])) + "_" + string.Format("{0:MMdd}", Convert.ToDateTime(Session["DateTo"]));
        //lblGroup.Text = Convert.ToString(ddlGroups.SelectedItem);
        //lblLocation.Text = Convert.ToString(ddlLocations.SelectedItem);
        //lblDateRange.Text = "From " + txtDateFrom.Text + " to " + txtDateTo.Text;
        if (grvExport.Rows.Count > 0)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=StatementPaymentsReport_" + dateRange + ".csv");
            //Response.Charset = "ISO-8859-1";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "text/csv; charset=utf-8";

            StringBuilder sb = new StringBuilder();
            sb.Append("\uFEFF");

            sb.Append("\"Region: " + HttpUtility.HtmlDecode(Convert.ToString(ViewState["Region"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n");
            sb.Append("\"Location: " + HttpUtility.HtmlDecode(Convert.ToString(ViewState["Location"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n");
            sb.Append("\"Group: " + HttpUtility.HtmlDecode(Convert.ToString(ViewState["Group"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n");
            sb.Append("\"From " + HttpUtility.HtmlDecode(Convert.ToString(Session["DateFrom"])).Replace("\"", "\"\"") + " to " + HttpUtility.HtmlDecode(Convert.ToString(Session["DateTo"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n"); 

            for (int i = 0; i < grvExport.Columns.Count; i++)
            {
                sb.Append('"' + grvExport.Columns[i].HeaderText + '"' + ',');
            }
            sb.Append("\r\n");

            for (int a = 0; a < grvExport.Rows.Count; a++)
            {
                for (int col = 0; col < grvExport.Columns.Count; col++)
                {
                    string value;
                    //if (col == 4)
                    //    value = '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[col].FindControl("lblAmount")).Text.Replace(Session["GeneralCurrency"].ToString(), string.Empty)) + '"' + ',';
                    //else
                    // value = '"' + HttpUtility.HtmlDecode(grvExport.Rows[a].Cells[col].Text) + "\","
                              value =  HttpUtility.HtmlDecode(grvExport.Rows[a].Cells[col].Text);
                    //string value = '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[0].FindControl("lblDate")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[1].FindControl("lblReferrer")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[2].FindControl("lblLocationName")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[3].FindControl("lblInvoice")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[4].FindControl("lblOrderType")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[5].FindControl("lblAmount1")).Text.Replace(Session["GeneralCurrency"].ToString(), string.Empty)) + '"' + ',';

                    // value = value.Replace("\"", "\"\"");
                        value = value.Replace("\"", "\"\"");

                        sb.Append('"' + value.Trim() + '"' + ',');
                   // sb.Append(value);
                   
                }
                sb.Append("\r\n");
            }

            //footer 09262016
            for (int i = 0; i < grvExport.Columns.Count; i++)
            {
                string value = grvExport.FooterRow.Cells[i].Text.Replace(" ", string.Empty);
                sb.Append('"' + value + "\",");
            }
            Response.Output.Write(sb.ToString());
            Response.Flush();
            Response.End();
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('No records to export!');", true);
        }

    }

    protected void lnkExportWOCSV_Click(object sender, EventArgs e)
    {
        var dateRange = string.Format("{0:MMdd}", Convert.ToDateTime(Session["DateFrom"])) + "_" + string.Format("{0:MMdd}", Convert.ToDateTime(Session["DateTo"]));
        //lblGroup.Text = Convert.ToString(ddlGroups.SelectedItem);
        //lblLocation.Text = Convert.ToString(ddlLocations.SelectedItem);
        //lblDateRange.Text = "From " + txtDateFrom.Text + " to " + txtDateTo.Text;
        if (grvExport.Rows.Count > 0)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=StatementPaymentsReport_" + dateRange + ".csv");
            //Response.Charset = "ISO-8859-1";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "text/csv; charset=utf-8";

            StringBuilder sb = new StringBuilder();
            sb.Append("\uFEFF");

            sb.Append("\"Region: " + HttpUtility.HtmlDecode(Convert.ToString(ViewState["Region"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n");
            sb.Append("\"Location: " + HttpUtility.HtmlDecode(Convert.ToString(ViewState["Location"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n");
            sb.Append("\"Group: " + HttpUtility.HtmlDecode(Convert.ToString(ViewState["Group"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n");
            sb.Append("\"From " + HttpUtility.HtmlDecode(Convert.ToString(Session["DateFrom"])).Replace("\"", "\"\"") + " to " + HttpUtility.HtmlDecode(Convert.ToString(Session["DateTo"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n");

            for (int i = 0; i < grvExport.Columns.Count; i++)
            {
                sb.Append('"' + grvExport.Columns[i].HeaderText + '"' + ',');
            }
            sb.Append("\r\n");

            for (int a = 0; a < grvExport.Rows.Count; a++)
            {
                for (int col = 0; col < grvExport.Columns.Count; col++)
                {
                    string value;
                    //if (col == 4)
                    //    value = '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[col].FindControl("lblAmount")).Text.Replace(Session["GeneralCurrency"].ToString(), string.Empty)) + '"' + ',';
                    //else
                    // value = '"' + HttpUtility.HtmlDecode(grvExport.Rows[a].Cells[col].Text) + "\","
                    value = HttpUtility.HtmlDecode(grvExport.Rows[a].Cells[col].Text);
                    //string value = '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[0].FindControl("lblDate")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[1].FindControl("lblReferrer")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[2].FindControl("lblLocationName")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[3].FindControl("lblInvoice")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[4].FindControl("lblOrderType")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[5].FindControl("lblAmount1")).Text.Replace(Session["GeneralCurrency"].ToString(), string.Empty)) + '"' + ',';

                    // value = value.Replace("\"", "\"\"");
                    value = value.Replace("\"", "\"\"");

                    sb.Append('"' + value.Trim() + '"' + ',');
                    // sb.Append(value);

                }
                sb.Append("\r\n");
            }

            //footer 09262016
            for (int i = 0; i < grvExport.Columns.Count; i++)
            {
                string value = grvExport.FooterRow.Cells[i].Text.Replace(" ", string.Empty);
                sb.Append('"' + value + "\",");
            }
            Response.Output.Write(sb.ToString());
            Response.Flush();
            Response.End();
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('No records to export!');", true);
        }
    }





这是我按lnkExportCSVWO时发生的错误。 This is in inspect element in browser:



Here is the error that happens when I press lnkExportCSVWO. This is in inspect element in browser:

Uncaught Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.
    at Function.Error$create [as create] (ScriptResource.axd:237)
    at Sys$WebForms$PageRequestManager$_createPageRequestManagerParserError [as _createPageRequestManagerParserError] (ScriptResource.axd:665)
    at Sys$WebForms$PageRequestManager$_parseDelta [as _parseDelta] (ScriptResource.axd:1435)
    at Sys$WebForms$PageRequestManager$_onFormSubmitCompleted [as _onFormSubmitCompleted] (ScriptResource.axd:1314)
    at Array.<anonymous> (ScriptResource.axd:47)
    at ScriptResource.axd:3484
    at Sys$Net$WebRequest$completed [as completed] (ScriptResource.axd:6373)
    at XMLHttpRequest.Sys$Net$XMLHttpExecutor._onReadyStateChange (ScriptResource.axd:5993)





For try-catch in the program:



For try-catch in the program:

ex = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}





lnkExportCSVWO is not working but lnkExportCSV is working and they both have the same codes. Both link buttons should export a CSV from a Gridview.



What I have tried:



Haven’t found a solution yet in google that is the same as mine.



lnkExportCSVWO is not working but lnkExportCSV is working and they both have the same codes. Both link buttons should export a CSV from a Gridview.

What I have tried:

Haven't found a solution yet in google that is the same as mine.

推荐答案

create [as create] (ScriptResource.axd:237)
at Sys
create [as create] (ScriptResource.axd:237) at Sys


WebForms


PageRequestManager
PageRequestManager


这篇关于导出到CSV不能在一个按钮上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆