Results 1 to 8 of 8

Thread: [RESOLVED] Printing

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    46

    Resolved [RESOLVED] Printing

    Hey Everyone, so I need to be able to print a friendly version of the page I am working on. I have done the CSS page for it and it works. However I would like to add a button that the user can click to print. Now i know that some browsers may not allow the button to work, but i know you can use javascript to do it. Does anyone know how the the code should look for this, or know any way that it might work other then javascript or selecting file/print. i would like to bring up the print dialog box if i can but not that important.

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Printing

    Hello,

    The necessary JavaScript code would simply be:

    Code:
    window.print();
    See here:

    http://www.w3schools.com/jsref/met_win_print.asp

    For more information.

    Gary

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    46

    Re: Printing

    Yeah I got it working, I didn't put in the code I was javascript anywhere. just put window.print() in the code. It didn't wanna seem to work on an ASP button, only an html button so I had to do some more coding, made a fucntion in javascript and used some InnerText to help label the buttons with what I wanted them called.

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Printing

    Hello,

    To be clear:

    Code:
    window.print();
    Is a JavaScript method. As a result, it will have to be called from the client side. The server side click event of an ASP.Net button will know nothing about this method, and as a result, you will not be able to call it directly. There are certainly techniques available that would allow you to call it from a server side ASP.Net Button, but the better approach would be to simply call the client side method/function that you create. You could have done this using the OnClientClick property of the ASP.Net Button control, or, as you have pointed out, simply use an HTML button. However, bear in mind that when rendered to the screen, an ASP.Net Button control is nothing more than an HTML button.

    Gary

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    46

    Re: [RESOLVED] Printing

    Yeah, I didn't realize i had it set to an ASP.NET button, what I did was to create a javascript function to print
    Code:
    <script type="text/javascript">
            function printpage() 
            {
                window.print()
            }
        </script>
    and with the onclick I call printpage() and it prints the page i am currently looking at, oh I also changed the button back to a regular html button, I probably didn't need to create the function, could have just put window.print() in the onclick.
    Last edited by jdogg; Feb 8th, 2012 at 08:34 AM. Reason: added text

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Printing

    Quote Originally Posted by jdogg View Post
    I probably didn't need to create the function, could have just put window.print() in the onclick.
    That would be correct. However, abstracting the method call into it's own function is certainly the "best practice" approach. That way, if you needed to, you could take this "Helper" function, and place it in a separate .js file, and then you could share this .js file in other applications.

    Gary

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    46

    Re: [RESOLVED] Printing

    Yeah that is true Gary, i am also just curious, the print fucntion I have works great, but do you know a way that i could call the print preview instead, let the user know what it will look like. If not I will just leave the code as is, but I think it would be a nice feature to give. The whole printing thing is just a bonus that we are providing to the client, so anything that I could do to make it look great woule be awesome

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Printing

    If you search for "asp.net print preview" in Google, you will find some results, but I have never tried any of them. Having read some of them, I would expect that you will find that it might work in some browsers, and not in others.

    Worth giving it a try though.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width