Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Print to client PC's printer

Print to client PC's printer

Scheduled Pinned Locked Moved ASP.NET
csharpquestionasp-net
12 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kani98
    wrote on last edited by
    #1

    I have an ASP.Net web application using C# and I would like to know how do I print what is currently displayed on the web page to the client PC's printer by clicking a button on the web page, thanks.

    M 1 Reply Last reply
    0
    • K kani98

      I have an ASP.Net web application using C# and I would like to know how do I print what is currently displayed on the web page to the client PC's printer by clicking a button on the web page, thanks.

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      You can use the javascript function window.print(); to initiate a client-side print dialog. So you could have a normal HTML button like this:

      <input type="button" onclick="window.print();"
      value="Print"
      />

      or an ASP.NET server control button like this:

      <asp:Button id="btnPrint" runat="server"
      Text="Print"
      OnClientClick="window.print();"
      />

      The advantage (or disadvantage?) of the latter is that it will still post back to the server. This could be useful if you wanted to log the fact that the user used the Print functionality from the button. You could eliminate the postback in the server control by adding return false; to the OnClientClick code:

      <asp:Button id="btnPrint" runat="server"
      Text="Print"
      OnClientClick="window.print(); return false;"
      />

      J 1 Reply Last reply
      0
      • M Mike Ellison

        You can use the javascript function window.print(); to initiate a client-side print dialog. So you could have a normal HTML button like this:

        <input type="button" onclick="window.print();"
        value="Print"
        />

        or an ASP.NET server control button like this:

        <asp:Button id="btnPrint" runat="server"
        Text="Print"
        OnClientClick="window.print();"
        />

        The advantage (or disadvantage?) of the latter is that it will still post back to the server. This could be useful if you wanted to log the fact that the user used the Print functionality from the button. You could eliminate the postback in the server control by adding return false; to the OnClientClick code:

        <asp:Button id="btnPrint" runat="server"
        Text="Print"
        OnClientClick="window.print(); return false;"
        />

        J Offline
        J Offline
        jcrussell
        wrote on last edited by
        #3

        This is cool. Although I suppose you would actually want to navigate to a printer-friendly page before you print, then on page.load you would call window.print(); ? Jason

        M 1 Reply Last reply
        0
        • J jcrussell

          This is cool. Although I suppose you would actually want to navigate to a printer-friendly page before you print, then on page.load you would call window.print(); ? Jason

          M Offline
          M Offline
          Mike Ellison
          wrote on last edited by
          #4

          Sounds good to me :)

          K 1 Reply Last reply
          0
          • M Mike Ellison

            Sounds good to me :)

            K Offline
            K Offline
            kani98
            wrote on last edited by
            #5

            I implemented your suggestion, but it doesn't print out everything that I want. What I have on the page are checkboxes, a dropdownlist box that allows me to select an hourly interval, a button that said "Generate Report" and another button that said "Print". I also have a DataGrid that is initially invisible. After selecting the checkboxes and pressing the "Generate Report" button, the DataGrid becomes visible displaying a report in tabular form. When I click the print button after pressing the generate report button, the printout only shows the checkboxes with nothing selected, the dropdownlist box with the default selection, and the two buttons. The DataGrid is not printed. How do I get it to print what is currently displayed on the page instead of what was displayed before I clicked the "Generate Report" button, thanks.

            M 1 Reply Last reply
            0
            • K kani98

              I implemented your suggestion, but it doesn't print out everything that I want. What I have on the page are checkboxes, a dropdownlist box that allows me to select an hourly interval, a button that said "Generate Report" and another button that said "Print". I also have a DataGrid that is initially invisible. After selecting the checkboxes and pressing the "Generate Report" button, the DataGrid becomes visible displaying a report in tabular form. When I click the print button after pressing the generate report button, the printout only shows the checkboxes with nothing selected, the dropdownlist box with the default selection, and the two buttons. The DataGrid is not printed. How do I get it to print what is currently displayed on the page instead of what was displayed before I clicked the "Generate Report" button, thanks.

              M Offline
              M Offline
              Mike Ellison
              wrote on last edited by
              #6

              Well, the javascript window.print() should print out whatever the browser has rendered. I'm guessing your "Print" button is first posting back to the server?

              K 1 Reply Last reply
              0
              • M Mike Ellison

                Well, the javascript window.print() should print out whatever the browser has rendered. I'm guessing your "Print" button is first posting back to the server?

                K Offline
                K Offline
                kani98
                wrote on last edited by
                #7

                For my webpage, I am using the VS IDE to design the page and putting the C# code in the codebehind. When I click on the html tab in the designer to put in OnClientClick="window.print(); return false;" for my print button, the designer draws a red line underneath OnClientClick and the print button doesn't work. When I added this code in the codebehind, private void PrintBtn_Click(object sender, System.EventArgs e) { string strJScript2 = "window.print();"; Page.RegisterStartupScript("Alert", strJScript2); } then the print button works correctly when I am viewing the page from the server. When I am viewing the page from a client PC it would print whatever is on the page before I press the "Generate Report" button.

                M 1 Reply Last reply
                0
                • K kani98

                  For my webpage, I am using the VS IDE to design the page and putting the C# code in the codebehind. When I click on the html tab in the designer to put in OnClientClick="window.print(); return false;" for my print button, the designer draws a red line underneath OnClientClick and the print button doesn't work. When I added this code in the codebehind, private void PrintBtn_Click(object sender, System.EventArgs e) { string strJScript2 = "window.print();"; Page.RegisterStartupScript("Alert", strJScript2); } then the print button works correctly when I am viewing the page from the server. When I am viewing the page from a client PC it would print whatever is on the page before I press the "Generate Report" button.

                  M Offline
                  M Offline
                  Mike Ellison
                  wrote on last edited by
                  #8

                  Are you using ASP.NET 2.0?

                  K 1 Reply Last reply
                  0
                  • M Mike Ellison

                    Are you using ASP.NET 2.0?

                    K Offline
                    K Offline
                    kani98
                    wrote on last edited by
                    #9

                    No, I am using VS .Net 2003 with 1.1 framework.

                    M 1 Reply Last reply
                    0
                    • K kani98

                      No, I am using VS .Net 2003 with 1.1 framework.

                      M Offline
                      M Offline
                      Mike Ellison
                      wrote on last edited by
                      #10

                      That's why OnClientClick didn't work - that's a 2.0 attribute. Okay, then - try using a straight html/client way of hanlding this:

                      <input type="button" onclick="window.print();"
                      value="Print"
                      />

                      K 1 Reply Last reply
                      0
                      • M Mike Ellison

                        That's why OnClientClick didn't work - that's a 2.0 attribute. Okay, then - try using a straight html/client way of hanlding this:

                        <input type="button" onclick="window.print();"
                        value="Print"
                        />

                        K Offline
                        K Offline
                        kani98
                        wrote on last edited by
                        #11

                        Thanks alot for your help Mike, I got it working correctly.

                        K 1 Reply Last reply
                        0
                        • K kani98

                          Thanks alot for your help Mike, I got it working correctly.

                          K Offline
                          K Offline
                          kani98
                          wrote on last edited by
                          #12

                          I have a slight problem with the window.print() function. When there are more data than what my DataGrid can display within its define height, a vertical scrollbar appears. When the user clicks the print button, it only prints out what is currently display on the screen. Is there a way for me to get it to print all the data that is on the DataGrid instead of having the user scroll down on the DataGrid to print out the rest of the data? I have noticed that certain websites have a "Printer Friendly Version" button that when clicked will format all the information on the page so that all the contents can be printed. Is there a way to do this in C#, thanks.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups