2012-08-28 55 views
0

我已經使用C#在一個表格中創建了一個表格。我想在用戶單擊Web窗體中的打印按鈕時僅打印表格。使用多頁打印表格時再次創建標題

我已經在asp.net中實現了只打印div內容的Javascript。然而,這是有效的,因爲表格中有很多行,所以有很多頁面。

我希望在每個新頁面中再次顯示錶頭。我怎麼能這樣做呢?

我的代碼:
在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(); 
    } 

回答

0

我用方法創建了一個CSS樣式表:

thead { display:table-header-group } 

但是,此方法僅適用於瀏覽器IE和Firefox。

相關問題