0

我有一个 30 行的网格视图。我想将它打印成 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>

帮助???

4

2 回答 2

1

CSS page-break-after 无法跨浏览器工作

page-break-after: always;用 ie10 和 firefox 22 进行了测试——两​​者都适用;在 Chrome 30 中它不起作用。所以很难说为什么它在 Firefox 22 中对你不起作用。

如果您的用户群主要是 IE 和 firefox(比如我们公司)。这将起作用:

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

显然对 css pagebreak 的支持并不理想:

搜索 chrome 问题导致我发现问题99124 - 打印的表格内容不尊重分页 CSS 属性;报告于 2011 年 10 月 4 日,但仍未分类:Confirmed, not reviewed for priority and assignment. 似乎打印并不重要。

添加这个 css 也没有帮助:

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

更新 - 使用 Javascript 的不完整解决方法

一种解决方法可能是将表格拆分为多个表格 - 对于每个页面。此演示在用户单击按钮后将单个表分解为 n 个表。你也需要这个 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 输出。

于 2013-08-03T07:19:41.440 回答
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!!!
于 2013-08-19T04:08:31.157 回答