2011-10-07 127 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