0

i have a simple custom application which fetches records from a lists by a caml query and through an export button the user is able to get a csv file with the data.

Generally the export works fine. But after the export nothign works on the page. I mean it is no longer possible to click on buttons and no events are getting executed (button onclick event).

    private void ExportToCSV(SPListItemCollection items)
    {
        if (items != null && items.Count > 0)
        {
            string attachment = "attachment; filename=export.csv";
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", attachment);
            HttpContext.Current.Response.ContentType = "text/csv";

            string columnNames = "Region;Country;Company";
            HttpContext.Current.Response.Write(columnNames);
            HttpContext.Current.Response.Write(Environment.NewLine);

            foreach (SPListItem item in items)
            {
                WriteCompetitorInfoToCSV(item);
                HttpContext.Current.Response.Flush();
            }

            HttpContext.Current.Response.End();
        }
    }

    private void WriteCompetitorInfoToCSV(SPListItem item)
    {
        StringBuilder stringBuilder = new StringBuilder();
        AddSemicolon(new SPFieldLookupValue(item["fielda"] as String).LookupValue, stringBuilder);
        AddSemicolon(new SPFieldLookupValue(item["fieldb"] as String).LookupValue, stringBuilder);
        AddSemicolon(new SPFieldLookupValue(item["fieldc"] as String).LookupValue, stringBuilder);
        HttpContext.Current.Response.Write(stringBuilder.ToString());
        HttpContext.Current.Response.Write(Environment.NewLine);
    }

Why does this happen?

1

I had this same issue some days ago. Following code works for me to escape this problem.

    protected void Page_Load(object sender, EventArgs e) {
     string js = @"_spSuppressFormOnSubmitWrapper = true;";
     this.Page.ClientScript.RegisterStartupScript(this.GetType(), "js", js, true);
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.