Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I wrote print script as :

XML
<script type="text/javascript">
function PrintDivData(crtlid)
{
    var ctrlcontent = document.getElementById(crtlid);
    var printscreen = window.open('','','left=1,top=1,width=1,height=1,toolbar=0,scrollbars=0,status=0');
    printscreen.document.write(ctrlcontent.innerHTML);
    printscreen.document.close();
    printscreen.focus();
    printscreen.print();
    printscreen.close();
}
</script>

and
XML
<input id="btnPrint" name="Print" runat="server" type="button" value="button" onclick="javascript:PrintDivData('Print');" />
<div id="Print">
<!--Here is repeater -->
</div>

Now ,when I am printing the repeater it's opening in window and having very small size. I don't want to open window please help me,

Thanking you
mohd Wasif
Posted
Updated 28-May-11 2:11am
v2

Hi,

Try the following code.

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PrintDemo.aspx.cs" Inherits="PrintDemo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Print Div Demo</title>
    <script language="javascript" type="text/javascript">
        function printIframe(divId) {
            window.frames['frame1'].document.body.innerHTML = document.getElementById(divId).innerHTML;
            window.frames['frame1'].focus();
            window.frames['frame1'].print();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <b>Print Demo</b>
        <div id="divPrint">
            <h1>
                Sample text1</h1>
            <br />
            <br />
            <h2>
                Sample Text2</h2>
            <br />
            <h3>
                Sample Text3</h3>
        </div>
        <div id="divHidden">
            <iframe id="frame1" style="height: 0px; width: 0px;"></iframe>
        </div>
        <input id="btnPrint" type="button" value="Print" onclick="printIframe('divPrint')" />
        <div>
            <b>This text should not be printed</b>
        </div>
    </div>
    </form>
</body>
</html>
 
Share this answer
 
Now ,when I am printing the repeater it's opening in window and having very small size.
That's because you have asked it to be opened that way.
See this line:
C#
var printscreen = window.open('','','left=1,top=1,width=1,height=1,toolbar=0,scrollbars=0,status=0');

Now the parameters top, left (ie only), width, height decides the position and height and width of the window.
You have specified height and width value as 1. Min value is 100px. So the size of the window will be 100px by 100px.
Change these values according to your requirement and it should be fine.
And this link should also help you - Window open() Method[^]

Hope this helps!
 
Share this answer
 
Comments
r a m e s h 30-May-11 1:11am    
The OP doesn't want to show the popup when printing. But the popup cannot be avoided when printing the content of a div. The other way is that css can be used to print div without opening popup window(Reference: http://stackoverflow.com/questions/2184765/print-div-content-from-user-control-without-opening-a-new-window).

So I suggested to use a hidden Iframe to load the div content and print it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900