Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to print part of the page in asp.net

I have a modal popup containing a div. I have a button to let the user print the content of that div. window.print prints the whole page. how can I print only the contents of the div.

Thanks.

like image 540
parisa Avatar asked Apr 16 '13 09:04

parisa


3 Answers

use this script

<script language="javascript" type="text/javascript">
    function CallPrint(strid) {
        var prtContent = document.getElementById(strid);
        var WinPrint = window.open('', '', 'letf=0,top=0,width=800,height=100,toolbar=0,scrollbars=0,status=0,dir=ltr');
        WinPrint.document.write(prtContent.innerHTML);
        WinPrint.document.close();
        WinPrint.focus();
        WinPrint.print();
        WinPrint.close();
        prtContent.innerHTML = strOldOne;
    }
</script>

and call it from button

 <asp:button id="BtnPrint" runat="server" onclientclick="javascript:CallPrint('bill');" text="Print" xmlns:asp="#unknown" />

your div also should has name (bill)

like image 53
hadi Avatar answered Sep 28 '22 20:09

hadi


This code may help u..

HTML Code:

 <div id="printarea">

    //content you want to print
    </div>

    <input id="btnprint" type="button" onclick="PrintDiv()" value="Print" /></center>

This is javascript

 <script type="text/javascript">

            function PrintDiv() {
                var divToPrint = document.getElementById('printarea');
                var popupWin = window.open('', '_blank', 'width=300,height=400,location=no,left=200px');
                popupWin.document.open();
                popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
                popupWin.document.close();
            }
         </script>
like image 38
sp_m Avatar answered Sep 28 '22 20:09

sp_m


try this:

function printdiv(printpage)
{
  var headstr = "<html><head><title></title></head><body>";
  var footstr = "</body>";
  var newstr = document.all.item(printpage).innerHTML;
  var oldstr = document.body.innerHTML;
  document.body.innerHTML = headstr+newstr+footstr;
  window.print();
  document.body.innerHTML = oldstr;
  return false;
}

printpage argument is your div id.

source: Microsoft forums: http://forums.asp.net/t/1261525.aspx/1

like image 23
Davide Piras Avatar answered Sep 28 '22 18:09

Davide Piras