c# - 如何滚动打印gridview?

标签 c# asp.net visual-studio-2010 gridview printing-web-page

我正在制作一个使用滚动的 gridview,因为列比我网页的页面宽度还多。 问题是当我想打印时它只打印显示的数据。我如何打印 div 或面板中的所有内容?

我的 GRIDVIEW 代码:

<a href="#" onclick="printPartOfPage('content-middle')" >PRINT ME </a>

<asp:Panel ID="gridPanel" runat="server" Height="500px" Width="980px" ScrollBars="Auto">


   <asp:GridView EnableSortingAndPagingCallbacks="true" Width="450px" 
            OnRowCreated="GridView1_RowCreated" runat="server" ID="GridView2" 
          CellPadding="4" ForeColor="#333333" GridLines="None" 
            >

                 <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

                 <Columns>

                     <asp:HyperLinkField DataNavigateUrlFields="ID" 
                         DataNavigateUrlFormatString="updateAppointmentOperations.aspx?showID={0}" Text="Update " 
                         Target="_blank" />
                 </Columns>

                 <EditRowStyle BackColor="#999999" />
                 <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                 <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                 <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                 <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                 <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                 <SortedAscendingCellStyle BackColor="#E9E7E2" />
                 <SortedAscendingHeaderStyle BackColor="#506C8C" />
                 <SortedDescendingCellStyle BackColor="#FFFDF8" />
                 <SortedDescendingHeaderStyle BackColor="#6F8DAE" />

             </asp:GridView>

    </asp:Panel>

我从互联网上得到的 javascript 打印是:

<script type="text/javascript">

function printPartOfPage(elementId) {
    var printContent = document.getElementById(elementId);
    var windowUrl = 'about:blank';
    var uniqueName = new Date();
    var windowName = 'Print' + uniqueName.getTime();
    var printWindow = window.open(windowUrl, windowName, 'left=50000,top=50000,width=0,height=0');

    printWindow.document.write('<link rel="stylesheet" type="text/css" href="style/additional.css" /><link rel="stylesheet" type="text/css" href="style/default.css" /><link rel="stylesheet" type="text/css" href="style/hi-res.css" />' + printContent.innerHTML);
    printWindow.document.close();
    printWindow.focus();
    printWindow.print();
    printWindow.close();
}

gridview 在一个 div 中,id 内容居中。

谢谢

最佳答案

您可以在页面上包含另一个 GridView,它将仅用于打印。

切换显示哪一个的方法是根据需要用 CSS 类装饰这两个。

例如:

<style>
@media print {
    .PrintOnly { display:block; }
    .ScreenOnly { display:none; }
  }
  @media screen {
    .PrintOnly { display:none; }
    .ScreenOnly { display:block; }
  }
</style>

关于c# - 如何滚动打印gridview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9776690/

相关文章:

asp.net - 命名 Dom 元素的 id 属性的最佳实践

c# - 如何订购列表<string>

c# - 具有通配符属性的 Specflow 步骤定义映射

c# - Entity Framework 忽略 NotMapped 属性

ASP.NET Core 2.0 Razor 与 Angular/React/等

visual-studio - VS 2010 最喜欢的功能

c# - 在 Xamarin IOS App 中使用 Hockey SDK 时链接失败

asp.net - 在编辑 View 中保留实体 key 的更安全方法?

c# - 发出 JSON 的 WCF Web 服务(菜鸟建议)

visual-studio-2010 - Powershell 命令构建后 VS2010