I copied a routine to print gridview and modified it to print formview. The problem I have is there are two formviews. The second may be empty. When both formviews contain data the routine works fine. When the second formview is empty the routine prints only the first two lines of the first formview and stops. I've tried everything I can think of but nothing seems to help. Any/all suggestions are greatly appreciated.
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