Server.Execute() executes click handler again after calling asp file

47 views Asked by At

I'm trying to generate a PDF using Winnovative Tools. In my click handler which fires after clicking on the "Print PDF button", I have the code below.

I expect control to resume to the printPDF_Click sub routine after calling Server.Execute() but instead it calls the printPDF_Click sub routine from scratch which causes a loop because Server.Execute() will be called again and so on.

It works as expected when I set preserveForm as False but then I lose my form data and the point is retaining it.

Private Sub printPDF_Click(sender As Object, e As EventArgs) Handles printPDF.Click
    Dim outTextWriter As IO.TextWriter = New IO.StringWriter()

    Server.Execute("Default_v3.aspx?isWinnovative=true", outTextWriter)

    Dim baseUrl As String = HttpContext.Current.Request.Url.AbsoluteUri
    Dim htmlStringToConvert As String = outTextWriter.ToString()


    Dim downloadBytes As Byte() = PdfHelper.CreatePdf(htmlStringToConvert, baseUrl)

    Dim response As HttpResponse = HttpContext.Current.Response
    response.Clear()
    response.AddHeader("Content-Type", "binary/octet-stream")
    response.AddHeader("Content-Disposition", ("attachment; filename=" + (
                     "Rendered.pdf" + ("; size=" + downloadBytes.Length.ToString))))
    response.Flush()
    response.BinaryWrite(downloadBytes)
    response.Flush()
    response.End()
End Sub
0

There are 0 answers