title: Download file from gridview rowcommand
body: <p>I've written out the below code to handle a file download which should occur on Gridview.RowCommand. It works in other places where I've used it (linkbutton outside of a gridview or similar control).</p>
  
  <p>This Gridview is inside of an UpdatePanel.</p>
  
  <pre><code>Protected Sub gvBikeInsurance_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvBikeInsurance.RowDataBound
      If e.Row.RowType = DataControlRowType.DataRow Then
          Dim ibtnExportToPDF As ImageButton = TryCast(e.Row.FindControl("ibtnExportToPDF"), ImageButton)
          ScriptManager.GetCurrent(Me).RegisterPostBackControl(ibtnExportToPDF)
  
          Dim btnDelete As LinkButton = TryCast(e.Row.Cells(e.Row.Cells.Count - 1).Controls(0), LinkButton)
          btnDelete.OnClientClick = "return confirm('Are you sure you want to delete this insurance item');"
      End If
  End Sub
  
  Protected Sub gvBikeInsurance_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvBikeInsurance.RowCommand
  
      Select Case e.CommandName
          Case "PDFExport"
              exportPolicy(e.CommandArgument)
      End Select
  End Sub
  
  Private Sub exportPolicy(ByVal BikeInsuranceID As Integer)
      Dim args As New List(Of MySqlParameter)
      args.Add(New MySqlParameter("xbikeinsuranceid", MySqlDbType.Int32))
      args(args.Count - 1).Value = BikeInsuranceID
  
      Dim dr As MySqlDataReader = db.execDB("InsuranceFiles_Select", CommandType.StoredProcedure, args.ToArray(), GeneralFunctions.ReturnType.DataReader, False)
  
      Dim output() As Byte
      If dr.HasRows Then
          dr.Read()
          output = dr("filedata")
      End If
      dr.Close()
  
      Dim outputstr As String = Text.Encoding.ASCII.GetString(output)
  
      Response.Clear()
      Response.ClearHeaders()
      Response.ContentType = "application/pdf"
      Response.AddHeader("Content-Disposition", String.Format("attachment;filename={0}", "Policy Schedule.pdf"))
  
      HtmlToPdf.ConvertHtml(outputstr, Response.OutputStream)
  
      Response.End()
  End Sub
  </code></pre>
  
  <p>The system captures user information from a form and writes HTML with it. This HTML is used to create a PDF file which is then supposed to be downloaded automatically.</p>
  
  <p>The problem is that the file doesn't seem to be downloading unless I run it through step by step through the debugger. Standard execution just displays the UpdateProgress content which disappears after a little while.</p>
  
  <p>Is there anything wrong with the code here? Most specifically in the RowDataBound and RowCommand routines and in the generation of Response.</p>
  
  <p>Any help will be greatly appreciated.</p>
  
  <p><strong>EDIT</strong>  </p>
  
  <p>Just spotted this error in Chrome's console:</p>
  
  <blockquote>
    <p>Uncaught Sys.WebForms.PageRequestManagerParserErrorException: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. </p>
  </blockquote>