Printing specified items from an Asp.net web form
I have an asp.net we开发者_StackOverflow中文版b page created with web forms. It has a number of gridviews on it which are populated from a database on a button click.
I am looking to print a version of this page on a different button click. I'm looking to customize the printed page, with some text that is specified, then the gridview contents (user friendly version), then some more text etc
Any help is greatly appreciated
Marc
The simplest solution is to use CSS
to show and hide different elements depending on whether they are intended for screen or print. Include all elements in the page output (both print and screen) and use CSS
to control them.
Here's a very simple example - let's say you have a summary text that should only be included in the print version, and some buttons that should only be included in the screen version.
Markup:
<p class="printOnly">
Here is my summary text.
</p>
<asp:button ID="btnSomeButton" runat="server" Text="Some Server Button" CssClass="screenOnly" />
CSS:
@media print {
.screenOnly { display: none; }
}
@media screen {
.printOnly { display: none; }
}
In the end any element marked with CSS
class screenOnly
will be hidden from print content, and any element marked with CSS
class printOnly
will be hidden from the screen version.
From there simply have a print button or link on the page somewhere with:
<a href="#" onclick="window.print();" class="screenOnly">Print Page</a>
You are probably using window.print()
to print the web form.
Create some div
's with the text you want inside them and hide the div
's initially.
When the print button is pressed, call a javascript function to unhide the div
's and call window.print()
. After window.print()
, hide the div
's again.
Simple way to print content of the web page is using Javascript.
<script language="JavaScript" type="text/JavaScript">
function doPrint() {
bdhtml=window.document.body.innerHTML;
sprnstr="<!--startprint-->";
eprnstr="<!--endprint-->";
prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
window.document.body.innerHTML=prnhtml;
window.print();
}
精彩评论