简体   繁体   中英

ASP.Net Print GridView

I am working on an ASP.Net Web Forms application. One page in particular displays a GridView which lists several rows of data. The GridView is styled using the built in formatting which you can choose within Visual Studio. However, I need to create a print friendly version of this webpage, but when I click print, the styling/ css of the GridView disappears.

Is there anyway of keeping the nice styling/ css that is visible when viewing as a webpage?

Thanks for you help.

You would use a print stylesheet:

@media print
{
    /* GridView styling here */
}

or link to a stylesheet specifically for print purposes:

<!--These styles styles are only for screens as set by the media value-->
<link rel="stylesheet" href="css/main.css" media="screen">

<!--These styles styles are only for print as set by the media value-->
<link rel="stylesheet" href="css/print.css" media="print">

Source: CSS Print Style Sheets - Examples

A while back I had a real problem with .net webforms insisting on adding style, border, spacing, padding attributes to the tables produced by Gridviews.

I used javascript (using jQuery) to remove them, like this:

$(document).ready(function () {  
$('table').filter(function (index) {  
    $(this).removeAttr("style");  
    $(this).removeAttr("rules");  
    $(this).removeAttr("border");  
    $(this).removeAttr("cellspacing");  
    $(this).removeAttr("cellpadding");  
});  
});  

This allowed my css (specified in the 'CssClass' attribute of the gridview) to take full control.

Perhaps this might be of some help in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM