title: ASP.Net Export List to CSV
body: <p>I have a very old asp.net application that uses C# .Net and JQuery Mobile, and I need to add 1 feature: <strong>Export a list of data to a csv file.</strong>  Thus far, I have the UI stuff sorted out.  I have a jquery button that passes a parameter in the request query string to indicate on page load that we need to export data to Excel.  I also have the following C# in the page load event.  Unfortunately, when I run this code in Visual Studio with Chrome (or any browser), I do not get a prompt to save the csv file. When I run it with IE, I get the following error: </p>
  
  <p>"Unhandled exception at line 2371, column 4 in <a href="http://localhost:53748/js/libs/jquery-mobile/jquery.mobile-1.0a4.1.js" rel="nofollow noreferrer">http://localhost:53748/js/libs/jquery-mobile/jquery.mobile-1.0a4.1.js</a></p>
  
  <p>0x800a138f - Javascript runtime error:Unable to get property '_trigger' of undefined or null reference."</p>
  
  <p>I'm not sure if I am handling everything with the response object correctly.</p>
  
  <pre><code>        if (Request.QueryString["isExportToExcelRequired"] == "true")
          {
              StringWriter sw = new StringWriter();
              sw.WriteLine("\"ItemID\",\"Description\",\"Price\"");
  
              if (MasterList.Count &gt; 0)
              {
                  for (int i = 0; i &lt; MasterList.Count; i++)
                  {
                      sw.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\"",
                                                 MasterList[i].item.ItemID,
                                                 MasterList[i].item.ItemDesc,
                                                 MasterList[i].P0
                                                 ));
                  }
  
                  string fileName = string.Format("{0}_{1}", SubClassID, DateTime.UtcNow.ToString("dd-MM-yyyy"));
  
                  // Write response. 
                  Response.ClearContent();
                  Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ".csv");
                  Response.ContentType = "text/csv";
                  Response.Write(sw.ToString());
                  Response.End();
              }
          }
  </code></pre>