如何打印formview页面?

时间:2014-10-07 23:26:13

标签: asp.net vb.net

我尝试使用formview表创建打印页面的例程。我有一个打印gridview的例程,它们基本相同吗?可以通过将GridView1更改为FormView1来修改它以使用formview吗?仅当formview处于只读模式时,这才有效。 VB是语言偏好。这位新手将非常感谢任何帮助。

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

我修改/更改了上面的内容,但是得到了一个&#34; System.Web.HttpException:Control&#39; FormView1&#39;类型&#39; FormView&#39;必须放在带有runat = server的表单标记内。&#34;错误信息,我现在完全迷失了如何解决它。该错误来自&#34; FormView1.RenderControl(hw)&#34;

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

1 个答案:

答案 0 :(得分: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
  1. 我需要将以下内容添加到aspx代码头:
  2. EnableEventValidation =&#34; false&#34;

    现在一切正常,我添加了一个类似的&#34;导出到Excel&#34;功能