2011-10-07 120 views
0

後顯示一個按鈕,我有一個創建頁面的PDF頁面上的按鈕。我想隱藏pdf的按鈕,然後在創建pdf後顯示它。我在代碼隱藏中嘗試了以下內容,但並未隱藏該按鈕。隱藏/點擊Asp.Net

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 

但是我在css中使用[@media print]不顯示我的打印按鈕。壞

回答

1

它不會隱藏按鈕的原因是因爲頁面不會再次這一行之前渲染:

PdfPageButton.Visible = True 

如果是的WinForms,你的方法是有效的,但在網絡上你需要做一點不同的事情。

你可以通過簡單的按鈕的onclick()事件設置的display:none CSS樣式隱藏按鈕:

<input type="button" id="pdfBtn" onclick="this.style.display = 'none';" /> 

但已經產生的PDF文件時再次顯示它,你要麼需要刷新頁面(即回發),或者如果您想使用AJAX,則可以連接事件監聽器。

編輯:鑑於PDF是頁面本身的額外信息,您是否可以向URL添加查詢字符串參數,例如,

mysite/mypage.aspx?isPDF=1

然後,在你PageLoad(),添加:

if(Request.QueryString["isPDF"] == "1") 
{ 
    PdfButton.Visible == false; 
} 

,使得按鈕時不isPDF被設置爲 '1'(或者不管你選擇)存在。

然後,通過 URL的額外參數爲ConvertURLToPDF()方法?

+0

你是對的,它確實隱藏了按鈕,但它仍然顯示在pdf上。 – TroyS

+0

哦對 - PDF是當前page_的PDF文件?我們需要知道它是如何生成的,但是我建議你使用CSS來搜索media =「print」 – Widor

+0

你是一個非常有效的天才。我添加了這個... Dim urlToConvert As String = HttpContext.Current.Request.Url.AbsoluteUri&「?IsPdf = 1」和查詢字符串到PageLoad事件並且Wa La工作。謝謝。 – TroyS

0

它是隱藏它,然後顯示它不會在其他方面的工作,但它不會顯示給客戶,因爲這一切都是在代碼隱藏發生前的頁面呈現給用戶。

你最好的選擇是增加一些客戶端腳本隱藏按鈕,當他們點擊它。當頁面重新生成時,該按鈕會再次出現,對他們可見。

1

,如果你想隱藏在Web瀏覽器按鈕,您必須使用JavaScript。 VBA是像php這樣的服務器端語言。您需要撥打ajax電話才能使用pdf。 當用戶點擊按鈕時,用javascript觸發動作,隱藏按鈕,發送請求到服務器,等待回答,然後再次顯示按鈕。

+0

換句話說在javascript函數中使用__doPostBack? – TroyS

+0

它基於您使用的JavaScript框架。 這裏是ajax的例子http://www.codeproject.com/KB/ajax/AjaxASPdotNET.aspx – Guntis

1

這將隱藏在客戶端上的按鈕,當您單擊按鈕。然後,當您的頁面呈現響應時,該按鈕應該再次顯示。

<asp:Button ID="PdfPageButton" OnClientClick="document.getElementById('PdfPageButton').style.display = 'none';" /> 
+0

@Bandon感謝答覆,我添加了標記,以我的按鈕,但不幸的是我沒有隱藏按鈕。 – TroyS