Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-29
8
Medium Priority
?
3,482 Views
Last Modified: 2011-10-03
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
0
Comment
Question by:CharlieDev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 22830071
you will have to use javascript to open the print dialog. web applications cannot send print commands directly to a local printer, this would be a huge security hole as someone could attack a companies printer's with massive print commands that would drive the printers to print tons of junk.

<input type="button" value="yourbutton" onclick="print()">

you need to have this javascript code on your page (placed in the head section of the html)

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

this will open a print dialog and when you submit it, the contents of the window will be sent to it(same thing as hitting print on the browser).

Source: http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_21495871.html
0
 

Author Comment

by:CharlieDev
ID: 22830201
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

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 22830213
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>

0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:CharlieDev
ID: 22830273
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

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 22830279
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#.
0
 

Author Comment

by:CharlieDev
ID: 22830451
ok thanks for your help, i will post back on here when i find the problem
0
 

Author Comment

by:CharlieDev
ID: 22831056
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

0
 

Author Closing Comment

by:CharlieDev
ID: 31511106
Cheers :)
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question