开发者

how to print the content of a label or panel in asp.net?

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 implem开发者_运维技巧ent that?

Please help.


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


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


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.


Use below code in button onclick event

ClientScript.RegisterStartupScript(this.GetType(), "PrintOperation", "PrintGridData()", true);

Above link will call function named PrintGridData() which is written in head section as below

<script type="text/javascript">
    function PrintGridData() 
    {
        var prtGrid = document.getElementById('<%=GridView.ClientID %>');
        prtGrid.border = 0;
        var prtwin = window.open('', 'PrintGridViewData', 'left=100,top=100,width=1000,height=1000,tollbar=0,scrollbars=1, status=0,resizable=1');
        prtwin.document.write(prtGrid.outerHTML);
        prtwin.document.close();
        prtwin.focus();
        prtwin.print();
        prtwin.close();
    }
</script>

in this script it will only print div name GridView and other part will not be printed

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜