title: Trying to solve error: Server cannot set content type after HTTP headers have been sent
body: <p>I know this question has been asked many times, and I have scoured those answers for a potential solution to what is happening with my code to no avail. So here goes.</p>
  
  <p>I have a page with an update panel. On this update panel I have a button that generates a Telerik pdf report and downloads it. However, when clicking the button I get the notorious error: Server cannot set content type after HTTP headers have been sent.</p>
  
  <p>Here is my code (simplified for ease of posting this question:</p>
  
  <pre><code>    protected void Page_Load(object sender, EventArgs e)
  { 
      ScriptManager.GetCurrent(this).RegisterPostBackControl(btnPayrollDetails);
  }
  
      protected void btnPayrollDetails_Click(object sender, EventArgs e)
  {
      PayPeriod pay = GetPeriod();
      if (pay == null || (pay.Expenses.Count == 0 &amp;&amp; pay.Invoices.Count == 0))
      {
          return;
      }
      var file = "test";
      var report = new Core.Reports.PayrollDetails(Global.Account.Member.ID, pay, mypMonth.SelectedDate.Value);
      Telerik.Reporting.Processing.RenderingResult result = new Telerik.Reporting.Processing.ReportProcessor().RenderReport("PDF", report, null);
      this.Response.DownloadFile(result.DocumentBytes, file + ".pdf");
  }
  </code></pre>
  
  <p>The call to this.Response.DownloadFile calls an extension method here:</p>
  
  <pre><code>public static void DownloadFile(this HttpResponse Response, byte[] fileBytes, string fileName, string mime = "application/pdf")
  {
      Response.Clear();
      Response.BufferOutput = true;
      Response.ContentType = mime;
      Response.AddHeader("content-disposition", "attachment; filename=\"" + fileName + "\"");
      using (MemoryStream ms = new MemoryStream(fileBytes)) { ms.WriteTo(Response.OutputStream); }
      HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
      HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
      HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
  }
  </code></pre>
  
  <p>The baffling part is I have this exact same code running in other projects and it works absolutely without any issues. So, if anyone could point out what I am doing wrong, I would greatly appreciate it.</p>