thirunavukaras 0 Newbie Poster

Hai...

Problem in Print Friendly Version with treeview control...

i am creating one web applications..

in this web application in main page The left side one panel and right side one Iframe control..

i am go to run the page the treeview control to expand corresponding page target to iframe control..

function PrintThisPage()
{
var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,";
sOption+="scrollbars=yes,width=750,height=600,left=100,top=25";


var sWinHTML = document.getElementById('PrintVersion').innerHTML;
alert(sWinHTML);


var winprint=window.open("","",sOption);
winprint.document.open();
winprint.document.write('<html><LINK href=/eggheadcafe.css rel=Stylesheet><body>');
winprint.document.write(sWinHTML);
winprint.document.write('</body></html>');
winprint.document.close();
winprint.focus();
}


<table id="tblTreeview" height="40" cellSpacing="0" cellPadding="0" width="98%" border="0">
<tr height="500" width="100%">
<td vAlign="top" width="30%"><asp:panel id="pnlHeading" runat="server" ForeColor="Maroon" Height="500" BackColor="PaleGoldenrod"
BorderStyle="None">
<iewc:TreeView id="TreeView1" runat="server" Width="256px">
<iewc:TreeNode Text="Forza">
<iewc:TreeNode Text="CarBoooking">
<iewc:TreeNode NavigateUrl="pgRptCarBooking.aspx" ImageUrl="RptImages\html1.gif" Text="RptCarBooking"
Target="ifrmTreeView"></iewc:TreeNode>
</iewc:TreeNode>
<iewc:TreeNode Text="TripClose">
<iewc:TreeNode NavigateUrl="pgRptTripClose.aspx" ImageUrl="RptImages\html1.gif" Text="RptTripClose"
Target="ifrmTreeView"></iewc:TreeNode>
</iewc:TreeNode>
</iewc:TreeNode>
</iewc:TreeView>
</asp:panel></td>
<td vAlign="top" width="70%">
<div id="PrintVersion" align="center">
<iframe id="ifrmTreeView" tabIndex="3" name="ifrmTreeView" width="677" height="500">
</iframe>
</div>
</td>
</tr>
</table>

this is the code

the problem i want to print the page in corresponding targeted page..

but the output shows iframe only..

there is no nothing is display..

please help

how to print the targeted page using treeview

please reply me..

Dani AI

Generated

— the empty output happens because reading the container's HTML gives you the <iframe> element only, not the document loaded inside it. To print the page that the TreeView targets you must print the iframe's window/document (if the target page is same-origin), or open the iframe's URL in its own window and print from there.

A simple same-origin approach — call print on the iframe's window after it has loaded:

function printIframeById(iframeId) {
  var frame = document.getElementById(iframeId);
  if (!frame) return;
  var win = frame.contentWindow || (frame.contentDocument && frame.contentDocument.defaultView);
  if (!win) { alert('Cannot access iframe window (cross-origin?)'); return; }
  win.focus();
  win.print();
}

If the iframe content is on a different origin, or you prefer a fresh window (avoids some browser quirks), open its URL and print when that window finishes loading:

function openUrlAndPrint(url) {
  var w = window.open(url, '_blank');
  w.onload = function() { w.focus(); w.print(); };
}

Troubleshooting / tips:

  • Same-origin restriction: accessing contentWindow or contentDocument requires identical protocol, host, and port. See the same-origin policy on MDN: Same-origin policy.
  • Wait for the iframe page to finish loading (use the iframe onload) before calling print().
  • Pop-up blockers can prevent window.open; ensure the print action is user-initiated.
  • Prefer adding a print button inside the report pages themselves (the pages loaded into the iframe). That guarantees styles and resources are available and avoids cross-window issues.
  • Use a print stylesheet (@media print) inside the report pages so printed output looks correct.

More on the APIs used: HTMLIFrameElement.contentWindow and Window.print().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.