Link to home
Start Free TrialLog in
Avatar of CharlieDev
CharlieDevFlag for United Kingdom of Great Britain and Northern Ireland

asked on

print an asp.net page using javascripts window.print()

Hi,

What code do I need to use with the window.print() bit on my asp.net page to print whats on that page?

i have this
 <asp:button ID="Button1" runat="server" text="Print" OnClientClick="window.print();return false"/>

Do i need to put some code in the head of my page or in the aspx.cs page?
Thanks
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CharlieDev

ASKER

Thanks but i get the error in a popup box of stack overflow at line 12

  <script type="text/javascript">
       function print()
       {
              window.print();
       }
</script>

line 12 being -  window.print();

I had to put script type="text/javascript"> as it complained with your code that i didnt have a type

Any ideas?

Thanks

To add javascript to your page so that users can print the content from your site is very simple, you can use an image instead of text to be clicked on in order to print but the general idea of what you should be doing is below, together with an example.

<p><a href="#" onClick="javascript:window.print()">Print this page</a></p>

Still get the same error message
<head runat="server">
    <title>Untitled Page</title>
      <link href="../css/default.css" type="text/css" rel="stylesheet" />
      <script type="text/javascript">
       function print()
       {
              window.print();
       }
</script>
</head>
<body>
<div class="PrintContainerFixtures">
    <form id="form1" runat="server">
                                
                                    <div class="colu1">
                                        <h5><asp:Label ID="Monthfixtureslabel" runat="server" Text=""></asp:Label></h5>
                                       <a href="#" onClick="javascript:window.print()">Print this page</a>
                                    </div>
                   
                            
                                        <div class="PrintGrid">
                                            <asp:GridView ID="gvwDisplayFixtures" GridLines="Both" runat="server" AutoGenerateColumns="False" showheader="true"
                                                Border="1" CssClass="gridPrintDisplayFixtures">
                                                <Columns>
                                                    <asp:BoundField DataField="Date" ItemStyle-CssClass="printdateHeaderFix" HeaderText="Date" />
                                                    <asp:BoundField DataField="Day" ItemStyle-CssClass="printdayHeaderFix" HeaderText="Day" />
                                                    <asp:BoundField DataField="Event" ItemStyle-CssClass="printeventHeaderFix" HeaderText="Event"/>
                                                    <asp:BoundField DataField="Time" ItemStyle-CssClass="printtimeHeaderFix" HeaderText="Time"/>
                                                </Columns>
                                            </asp:GridView>
                                            <asp:Label ID="lblStatus" runat="server"></asp:Label>
                                        </div>
                                       </form>
                                    </div>
                             
                            
</body>
</html>

Open in new window

Based on what you've posted, I can tell you this much: A Stack Overflow is almost always caused by an infinite loop. It has nothing to do with JavaScript, printing, or C#.
ok thanks for your help, i will post back on here when i find the problem
Working code below, source- http://visualstudiomagazine.com/columns/article.aspx?editorialsid=1907
 <script language="JavaScript"><!--
function PrintContent() {
   parent.Content.focus();  
   // Required to support IE
   parent.Content.print();
}
//--></script>
</head>
<body>
<div class="PrintContainerFixtures">
    <form id="form1" runat="server">
                                
                                    <div class="colu1">
                                        <h5><asp:Label ID="Monthfixtureslabel" runat="server" Text=""></asp:Label></h5>

Open in new window

Cheers :)