maryam mohammadi
maryam mohammadi

Reputation: 694

Div content is missed

i have a div with a table object which fill in onload event of the page.

<div id="divcontent" runat="server">
<asp:Table id="tblcontent" runat="server" CellPadding="4" align="center"   width="80%">      </asp:Table>
</div>     

<div align="left" style ="width:90%; padding-top:20px;">
<input value="Print" type="button" onclick="PrintDiv();"/>       
</div> 

and i have bellow code to open a popup window with div content in order to print just the div content:

<script type="text/javascript">

function PrintDiv() {

    var divToPrint = document.getElementById('divcontent');

    var popupWin = window.open('', '_blank', 'width=500,height=700');

    popupWin.document.open();

    popupWin.document.write('<html><body onload="window.print()">' +   divToPrint.innerHTML + '</html>');

    popupWin.document.close();
}

</script> 

but when the popup window opens, its empty!! Could anybody say why?

Upvotes: 0

Views: 128

Answers (1)

Yograj Gupta
Yograj Gupta

Reputation: 9869

If your page have a master page, then the divcontent will not the id value on browser as you have set the runat="server", so use ClientID of the control to get the rendered id of div.

Try this,

function PrintDiv() {
    var divToPrint = document.getElementById('<%= divcontent.ClientID %>');

    var popupWin = window.open('', '_blank', 'width=500,height=700');
    popupWin.document.open();
    popupWin.document.write('<html><body onload="window.print()">' +   divToPrint.innerHTML + '</html>');
    popupWin.document.close();
}

Upvotes: 3

Related Questions