I have a button on a page that creates a PDF of the page. I would like to hide the button for the pdf then show it after the pdf is created. I tried the following in codebehind and it does not hide the button.
Private Sub PdfPageButton_ServerClick(sender As Object, e As System.EventArgs) Handles PdfPageButton.ServerClick
PdfPageButton.Visible = False
ConvertURLToPDF()
PdfPageButton.Visible = True
End Sub
Private Sub ConvertURLToPDF()
Dim urlToConvert As String = HttpContext.Current.Request.Url.AbsoluteUri
'more code here not displayed...
' Performs the conversion and get the pdf document bytes that you can further
' save to a file or send as a browser response
Dim pdfBytes As Byte() = pdfConverter.GetPdfBytesFromUrl(urlToConvert)
' send the PDF document as a response to the browser for download
Dim Response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
Response.Clear()
Response.AddHeader("Content-Type", "binary/octet-stream")
Response.AddHeader("Content-Disposition", "attachment; filename=ConversionResult.pdf; size=" & pdfBytes.Length.ToString())
Response.Flush()
Response.BinaryWrite(pdfBytes)
Response.Flush()
Response.End()
End Sub
However I use [@media print] in css to not display my print button. to bad it doesn’t work in other ways
The reason it doesn’t hide the button is because the page doesn’t get rendered again before this line:
If it were WinForms, your approach would work but on the web you need to do it a bit differently.
You could hide the button by simply setting a CSS style of
display:nonein the button’sonclick()event:but to show it again when the PDF has been generated, you’d either need to refresh the page in its entirety (i.e. a postback), or you could wire up an event listener if you wanted to use AJAX.
EDIT: Given the extra info that the PDF is of the page itself, could you perhaps add a querystring parameter to the URL, e.g.
mysite/mypage.aspx?isPDF=1Then, in your
PageLoad(), add:so that the button doesn’t exist when
isPDFis set to ‘1’ (or whatever you choose).Then, pass that URL with the extra param to the
ConvertURLToPDF()method?