Thursday, 4 April 2013

How to Print particular portion of webpage?

Here I am going to demonstrate “how to print particular portion of webpage?”. Manier  we use “Window.Print()” javascript in order to print data displayed on web-page. But there may be a case when I didn’t need to print whole page or just required to print one small portion of page instead of whole page, in those scenario “Window.print()” won’t work. So what should we do in such situation? Here is the answer.

Inside the code
We can print web document through javascript, so definitely we’ll write some javascript to print portion of web page. Here is the script:

        function CallPrint(strid) {
            var prtContent = document.getElementById(strid);
            var WinPrint ='', '', 'letf=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
            prtContent.innerHTML = strOldOne;

Now, how to use this javascript? For that create a <div /> element and assign unique id to it (i.e. <div ID=”divPrint”). Now put your html content between this <div />.
For example, if I want to print Item information which are displays on grid like:

<asp:DataGrid Id=”grdItem” runat=”server>

Put this whole html content between <div /> tag.
Now onClientClick event of button call javascript function like:
This all makes your printing done.