views:

454

answers:

3

Hi all,

I have a label control in a page in asp.net 2.0 and a button Print.

Clicking on print button I need to print the content of the label and I need same for a panel also.

Is it possible to implement this?

If yes then how to implement that?

Please help.

+1  A: 

You would need to add some client side javascript to your Print button to execute the brower's print command. The javascript below could be used to print the whole document page and would be a good place to start. Note: It isn't possible to do this without displaying the print dialog unless you use a third party component.

// Print Page   
window.print();

If you wanted just to print certain sections of your page you can achieve this two ways. Firstly, you could render the content to be printed into a hidden iframe and then print just that frame. You would do this using the same code as above only from within the frame itself.

Secondly, you could use a media style print style sheet, a CSS that applies only when printing. Inside this sheet you would set the styles you wanted to print as normal and the styles you didn't want to print to "display:none". The link below contains more information on print stylesheets.

http://www.webcredible.co.uk/user-friendly-resources/css/print-stylesheet.shtml

Lee Hesselden
+1  A: 

What about Window.Print() method? Because execcommand method will not work other browsers that IE. Use CSS media option to control the print area.

Anuraj
+1  A: 

One more approach could be to open new window and populate the contents of div you wanted to print and have print link/button on that page. Ex:

var win = window.open(...) win.document.body.appendChild(document.getElementById('divToPrintId'))

This is how the code will look like this approach is used to print the content/part of the page.

Anil Namde