打印具有多个页面的表时再次创建标题

时间:2012-08-28 06:52:51

标签: javascript c# asp.net printing

我使用C#在webform中创建了一个表。我想在用户单击webform中的打印按钮时仅打印表。

我在asp.net中实现了Javascript,其中只打印了div内容。但是,这有效,因为表中有很多行,所以有很多页面。

我希望在每个新页面中再次显示表格标题。我怎么能这样做呢?

我的代码:
在asp.net中 - 打印按钮,表

<asp:Button ID="printBtn" runat="server" Text="Print" onClientClick="printdiv('tableDiv')" />
            <div id="tableDiv">
            <asp:Table ID="resultsTable" runat="server" GridLines="Both">
            </asp:Table>
            </div>

在asp.net中 - 用于打印的javascript

 <script type="text/javascript">
        function printdiv(printpage) {
            var headstr = "Header";
            var footstr = "Footer";
            var newstr = document.all.item(printpage).innerHTML;
            var oldstr = document.body.innerHTML;
            document.body.innerHTML = headstr + newstr + footstr;
            window.print();
            document.body.innerHTML = oldstr;
            return false;
        }
        </script>

代码背后的表方法:

    protected void resultTable()
    {
        //database setup codes
        ...............

        try
        {
            //database query codes
            ............

            if (reader.HasRows)
            {
                //Set a table width.
                resultsTable.Width = Unit.Percentage(60.00);
                //Create a new row for adding a table heading.
                TableRow tableHeading = new TableRow();

                //Create and add the cells that contain the Name column heading text.
                TableHeaderCell nameHeading = new TableHeaderCell();
                nameHeading.Text = "Name";
                nameHeading.HorizontalAlign = HorizontalAlign.Left;
                tableHeading.Cells.Add(nameHeading);

                TableHeaderCell ageHeading = new TableHeaderCell();
                ageHeading.Text = "Age";
                ageHeading.HorizontalAlign = HorizontalAlign.Left;
                tableHeading.Cells.Add(ageHeading);

                resultsTable.Rows.Add(tableHeading);


                while (reader.Read())
                {

                    TableRow detailsRow = new TableRow();

                    TableCell nameCell = new TableCell();
                    nameCell.Text = reader["name"].ToString();
                    detailsRow.Cells.Add(nameCell);

                    TableCell ageCell = new TableCell();
                    ageCell.Text = reader["age"].ToString();
                    detailsRow.Cells.Add(ageCell);

                    resultsTable.Rows.Add(detailsRow);

                }
            }
            reader.Close();
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
        conn.Close();
    }

1 个答案:

答案 0 :(得分:0)

我用方法创建了一个CSS样式表:

thead { display:table-header-group }

但是,此方法仅适用于浏览器IE和Firefox。