2014-10-07 81 views
0

我正在嘗試創建一個例程來打印帶有formview表格的頁面。我有一個打印gridview的例程,它們基本上是一樣的嗎?通過將GridView1更改爲FormView1,可以修改它以使用formview嗎?這隻有在formview處於只讀模式時纔有效。 VB是語言偏好。任何幫助將不勝感激這個新手。如何打印formview頁面?

Protected Sub Print_Click(sender As Object, e As System.EventArgs) Handles Button1.Click 

    GridView1.AllowPaging = False 
    GridView1.DataBind() 
    GridView1.UseAccessibleHeader = True 
    GridView1.HeaderRow.TableSection = TableRowSection.TableHeader 
    GridView1.Attributes("style") = "border-collapse:separate" 
    For Each row As GridViewRow In GridView1.Rows 
     If row.RowIndex Mod 30 = 0 AndAlso row.RowIndex <> 0 Then 
      row.Attributes("style") = "page-break-after:always;" 
     End If 
    Next 
    Dim sw As New StringWriter() 
    Dim hw As New HtmlTextWriter(sw) 
    GridView1.RenderControl(hw) 
    Dim gridHTML As String = sw.ToString().Replace("""", "'").Replace(System.Environment.NewLine, "") 
    Dim sb As New StringBuilder() 
    sb.Append("<script type = 'text/javascript'>") 
    sb.Append("window.onload = new function(){") 
    sb.Append("var printWin = window.open('', '', 'left=0") 
    sb.Append(",top=0,width=1000,height=600,status=0');") 
    sb.Append("printWin.document.write(""") 
    Dim style As String = "<style type = 'text/css'>thead {display:table-header-group;} tfoot{display:table-footer-group;}</style>" 
    sb.Append(style & gridHTML) 
    sb.Append(""");") 
    sb.Append("printWin.document.close();") 
    sb.Append("printWin.focus();") 
    sb.Append("printWin.print();") 
    sb.Append("printWin.close();") 
    sb.Append("};") 
    sb.Append("</script>") 
    ClientScript.RegisterStartupScript(Me.[GetType](), "GridPrint", sb.ToString()) 
    GridView1.AllowPaging = True 
    GridView1.DataBind() 
End Sub 

我修整/改變了上方到下方以下但得到一個「System.Web.HttpException:控制‘FormView1’類型‘的FormView’必須放在與RUNAT =服務器form標籤的內部。」錯誤消息,現在完全失去了如何解決它。該錯誤來自「FormView1.RenderControl(hw)」行「

Protected Sub Button5_Click(sender As Object, e As System.EventArgs) Handles Button5.Click 
    FormView1.AllowPaging = False 
    FormView1.DataBind() 
    FormView1.HeaderRow.TableSection = TableRowSection.TableHeader 
    FormView1.Attributes("style") = "border-collapse:separate" 
    Dim sw As New StringWriter() 
    Dim hw As New HtmlTextWriter(sw) 
    FormView1.RenderControl(hw) 
    Dim gridHTML As String = sw.ToString().Replace("""", "'").Replace(System.Environment.NewLine, "") 
    Dim sb As New StringBuilder() 
    sb.Append("<script type = 'text/javascript'>") 
    sb.Append("window.onload = new function(){") 
    sb.Append("var printWin = window.open('', '', 'left=0") 
    sb.Append(",top=0,width=1000,height=600,status=0');") 
    sb.Append("printWin.document.write(""") 
    Dim style As String = "<style type = 'text/css'>thead {display:table-header-group;} tfoot{display:table-footer-group;}</style>" 
    sb.Append(style & gridHTML) 
    sb.Append(""");") 
    sb.Append("printWin.document.close();") 
    sb.Append("printWin.focus();") 
    sb.Append("printWin.print();") 
    sb.Append("printWin.close();") 
    sb.Append("};") 
    sb.Append("</script>") 
    ClientScript.RegisterStartupScript(Me.[GetType](), "GridPrint", sb.ToString()) 
    FormView1.AllowPaging = True 
    FormView1.DataBind() 
End Sub 
+0

'ClientScript.RegisterStartupScript','GridView1.DataBind'?你確定這是WPF代碼嗎?這看起來像一個ASP.net代碼。 – pushpraj 2014-10-08 01:33:18

+0

由於使用Template結構構建的FormView Table不能執行此處每30行執行的「page-break-after:always」邏輯。我不知道你的具體要求,但是對於基於FormView的表結構的情況,我建議你從f/e JavaScript開始,與上面例程中構建的相同,只有你必須處理的其他內容是page-break-之後:總是每30行使用JavaScript。 – 2014-10-08 04:35:34

回答

0

原來我需要做兩件事。 1.我需要例行添加到我的背後VB代碼:

Public Overrides Sub VerifyRenderingInServerForm(control As Control) 
    ' Confirms that an HtmlForm control is rendered for the specified ASP.NET  server control at run time. 
End Sub 
  • 我需要添加以下的ASPX代碼標頭:
  • EnableEventValidation =「false」

    現在一切正常,我添加了一個類似的「導出到Excel」功能。