当一个表单视图为空时打印多个表单视图会产生不完整的结果

时间:2014-10-09 22:56:47

标签: asp.net vb.net

我有一个包含两个表单的页面。当两个表单视图都包含数据时,下面的子项可以正常打印,但是当第二个formview为空时,我只获得第一个formview的部分输出。 sub的第一部分(如果FormView2.DataItemCount = 0 Then)是问题发生的地方。奇怪的是,如果运行第一部分(没有" If"语句),它只能正确打印第一个formview,忽略第二个。在这种情况下,第一个formview应该有17行数据,但只打印2行。在我变老和grayer之前,有没有人有任何建议?谢谢!

注意:我将默认浏览器从资源管理器更改为Firefox,程序运行正常。这显然是Explorer不喜欢的东西。

Protected Sub Button5_Click(sender As Object, e As System.EventArgs) Handles Button5.Click

    If FormView2.DataItemCount = 0 Then
        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.DataBind()
    End If

    If FormView2.DataItemCount <> 0 Then
        FormView1.DataBind()
        FormView1.HeaderRow.TableSection = TableRowSection.TableHeader
        FormView1.Attributes("style") = "border-collapse:separate"
        FormView2.DataBind()
        FormView2.HeaderRow.TableSection = TableRowSection.TableHeader
        FormView2.Attributes("style") = "border-collapse:separate"
        Dim sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)
        FormView1.RenderControl(hw)
        FormView2.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.close();")
        sb.Append("};")
        sb.Append("</script>")
        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 style1 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.DataBind()
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

我终于找到了一个给我答案的帖子,以及一个在Explorer和Firefox中都有效的简单解决方案。我删除了以上所有内容并将其更改为以下内容:

Protected Sub Button5_Click(sender As Object, e As System.EventArgs) Handles Button5.Click

    Page.ClientScript.RegisterStartupScript(Me.GetType(), "popup", "<script language=javascript>javascript:window.print();</script>")

End Sub