How to print part of a page in asp.net

I have a modal popup containing a div. I have a button that allows the user to print the contents of this div. window.print prints the entire page. how can I print only the contents of a div.

Thank.

+5
source share
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 name it with the button

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

your div should also have a name (account)

+5
source

try the following:

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;
}
Argument

printpage is your div id.

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

+2
source

This code can help you ...

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>
+2
source

All Articles