打印网格视图 - 如何使用分页符在每个页面上打印n行

时间:2013-08-03 06:08:15

标签: javascript asp.net gridview printing

我有一个30行的gridview。我想打印3页,每行10行。我试过这段代码。

        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowIndex % 10 == 0 && row.RowIndex != 0)
            {
                row.Attributes["style"] = "page-break-after:always;";
            }
        }

但是它打印整个页面没有任何分页符。我曾经使用javascript调用打印函数(如下所示)。

    function PrintGridData() {
        var prtGrid = document.getElementById('<%=GridView1.ClientID %>');
        prtGrid.border = 0;
        //GridView1.Attributes(style) = "page-break-after:always"         
        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();
    }

帮助???

问候......

我正在下面显示我的完整标记..请检查它..

<%@ Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="About.aspx.cs" Inherits="WebApplication2.About" EnableEventValidation="false" %>

<%@ Register assembly="Shared.WebControls" namespace="Shared.WebControls" tagprefix="cc1" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        About
    </h2>
    <script type="text/javascript">

        function PrintGridData() {
            var prtGrid = document.getElementById('<%=ReportGridView1.ClientID %>');

                //window.print();
            prtGrid.border = 0;
            //GridView1.Attributes(style) = "page-break-after:always"         
            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>
   <p>
        <asp:Button ID="Button1" runat="server" Text="Button" 
             onclick="Button1_Click" OnClientClick="PrintGridData()"/>
&nbsp;<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
            CellPadding="4" DataKeyNames="ID" DataMember="DefaultView" 
            ForeColor="Black" GridLines="None" 
            ShowFooter="True" style="margin-left: 0px; margin-right: 0px; margin-top: 3px;" 
            Width="965px" 
            ShowHeaderWhenEmpty="True" 
            EnableTheming="True" onrowcreated="GridView1_RowCreated" 
            onrowdatabound="GridView1_RowDataBound">
            <AlternatingRowStyle BackColor="White" BorderStyle="Solid" />
            <Columns>
                <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Select">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkrow" runat="server" AutoPostBack="True" 
                            TabIndex="1" ToolTip="Select" />
                    </ItemTemplate>
                    <HeaderTemplate>
                        <asp:CheckBox ID="chkhead" runat="server" />
                    </HeaderTemplate>
                </asp:TemplateField>
                <asp:CommandField ButtonType="Button" HeaderText="Edit" ShowEditButton="True">
                <ControlStyle CssClass="classname" />
                </asp:CommandField>
                <asp:TemplateField HeaderText="First Name">
                    <ItemTemplate>
                        <asp:Label ID="lblfirstname" runat="server" Text='<%# Eval("First_name") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txteditfirstname" runat="server" 
                            Text='<%# Eval("First_name") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Second Name">
                    <ItemTemplate>
                        <asp:Label ID="lblsecondname" runat="server" Text='<%# Eval("Second_name") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txteditsecondname" runat="server" 
                            Text='<%# Eval("Second_name") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="DOB">
                    <ItemTemplate>
                        <asp:Label ID="lbldob" runat="server" Text='<%# Eval("DOB") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txteditdob" runat="server" Text='<%# Eval("DOB") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Address">
                    <ItemTemplate>
                        <asp:Label ID="lbladdress" runat="server" Text='<%# Eval("Address") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txteditaddress" runat="server" Text='<%# Eval("Address") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="PIN">
                    <EditItemTemplate>
                        <asp:TextBox ID="txteditpin" runat="server" Text='<%# Eval("PIN") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblpin" runat="server" Text='<%# Eval("PIN") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Contact No">
                    <ItemTemplate>
                        <asp:Label ID="lblcontactno" runat="server" Text='<%# Eval("Contact_no") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txteditcontactno" runat="server" 
                            Text='<%# Eval("Contact_no") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="E-mail">
                    <ItemTemplate>
                        <asp:Label ID="lblemail" runat="server" Text='<%# Eval("E_mail")%>'></asp:Label>
                    </ItemTemplate>

                    <EditItemTemplate>
                        <asp:TextBox ID="txteditemail" runat="server" Text='<%# Eval("E_mail") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#CCCC99" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <PagerSettings Visible="False" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <RowStyle BackColor="#F7F7DE" />
            <SelectedRowStyle BackColor="#CE5D5A" CssClass="SelectedRowStyle" 
                Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FBFBF2" BorderStyle="Double" />
            <SortedAscendingHeaderStyle BackColor="#848384" />
            <SortedDescendingCellStyle BackColor="#EAEAD3" />
            <SortedDescendingHeaderStyle BackColor="#575357" />
        </asp:GridView>
</p>

帮助???

2 个答案:

答案 0 :(得分:1)

CSS page-break-after不能跨浏览器工作

我用ie10和firefox 22测试了page-break-after: always; - 它适用于两者;在Chrome 30中,它不起作用。所以很难说它为什么在firefox 22中不适合你。

如果您的用户群主要是IE和Firefox(就像我们公司一样)。这将有效:

.pagebreak {
    page-break-after: always;
}

显然对css分页功能的支持并不理想:

search for chrome issues引导我发出99124 - Printed table content does not respect page-break CSS properties; 2011年10月4日报道,仍然没有受到影响:Confirmed, not reviewed for priority and assignment。印刷似乎并不重要。

添加此css也无济于事:

@media print
{
table tr.pagebreak {page-break-after:always}
}

已更新 - 使用Javascript

的补丁变通方法

解决方法可能是将表拆分为多个表 - 对于每个页面。用户点击按钮后,此 demo breaks a single table into n tables 。你也需要这个css hr {page-break-after: always;}

我更新了javascript以在每个页面之间加入<hr />,甚至强制Chrome添加分页符。

function printTable(){
    var tables = createTables(2);
    $("#printTables").html(tables); 
}       

function createTables(rowsPerTable){            
    var rowsInParentTable  = $( "tbody tr" ).toArray();                 
    var subTable = "<table>", subRows=[], sr = 0;           
    for(i=0; i < rowsInParentTable.length; i++){                
       subTable += "<tr>" + rowsInParentTable[i].innerHTML + "</tr>";
       if( i % rowsPerTable === 0 && (i > rowsPerTable || i +1 > rowsPerTable ){
        subTable += "</table> <hr /><table>";
       }
    }
    return subTable;
}

和html

   <div>
     <table id="sourceTable">
       <tbody>
        <tr><td>1 Browser</td>
                <td>Version</td>
                <td>Support for 'page-break-after' </td></tr>
        <tr><td>2 Internet Explorer</td>
                <td>10</td><td>works for table rows</td></tr>
        <tr><td>3 Fire Fox</td><td>22</td>
                <td>works for table rows</td></tr>
            <tr><td>4 Chrome</td><td>30</td>
                <td>does not work</td></tr>
        <tr><td>5 Safari</td><td>?</td><td>?</td></tr>
        <tr><td>6 Opera</td><td>?</td><td>?</td></tr>
      </tbody>
     </table>
     <button onclick="printTable()">print table</button>
    </div>
    <div id="printTables">
    </div>

我的推荐

我会创建一个单独的aspx页面来处理打印页面。这样你就可以拥有完整的服务器端控制,并且可以使用转发器来创建你需要的html输出。

答案 1 :(得分:1)

if (!IsPostBack)
        {
            var sb = new StringBuilder();
            string s;
            show();
            for (int i = 0; i < GridView1.PageCount; i++)
            {
                GridView1.FooterRow.Visible = false;
                GridView1.PageIndex = i + 1;
                sb.Clear();
                GridView1.RenderControl(new HtmlTextWriter(new StringWriter(sb)));
                s = sb.ToString();               
                Response.Write(s);
                if (i != GridView1.PageCount - 1)
                {
                    Response.Write("<p></p>");
                }
                show();
            }
            GridView1.Visible = false;
        }

 public override void VerifyRenderingInServerForm(Control control)
 {

 }



I have used this method to solve the case..I have applied paging to the **gridview** and it works!!!