Tuesday, December 16, 2008

How to Get Printer option When a Button is Clicked?

The First thing to note here is when you click on the Button,the page content should be printed but the Button say PrintButton should not appear in the document that is going to get printed.


When you simply call window.print() , this problem occurs.

How to Solve this?

The Source Code for the Button created is as shown below:

<asp:Button ID="btnPrint" runat="server" CssClass="btn1" Text="Print Me"
OnClientClick="javascript:fun(this,'print')"/>

Observe that I am calling a function fun with 2 arguments ,i.e this and print.
'this' refers to Button with ID='btnPrint' i.e the Button which we want to hide after clicking.

The Second argument is nothing but Css ClassName.

The CssClass Code is as Below:

<style type="text/css">
.print
{
visibility:hidden;
}
.btn1
{
color:#050;
font: bold 84% 'trebuchet ms',helvetica,sans-serif;
background-color:#fed;
border:1px solid;
border-color: #696 #363 #363 #696;
}
</style>

The JavaScript function fun will be as shown below:

<script type="text/javascript" language="javascript">
function fun(loc,p)
{
loc.className=p
window.print();
}

</script>

No comments: