【问题标题】:How do I print a formview page?如何打印表单视图页面?
【发布时间】:2014-10-07 23:26:13
【问题描述】:

我正在尝试创建一个例程来打印带有 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

我将上面的内容修剪/更改为下面的内容,但得到“System.Web.HttpException:'FormView' 类型的控件 'FormView1' 必须放在带有 runat=server 的表单标签内。”错误消息,现在完全不知道如何修复它。错误来自“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

【问题讨论】:

  • ClientScript.RegisterStartupScript, GridView1.DataBind?你确定这是WPF代码吗?这看起来像一个 ASP.net 代码。
  • 作为使用模板结构构建的 FormView 表,您无法执行每 30 行执行一次的“page-break-after:always”逻辑。我不知道您的确切要求,但对于您基于 FormView 的表结构案例,我建议您从 f/e JavaScript 中使用您在上述例程中构建的相同内容,您必须处理的唯一额外事情是分页符-之后:总是使用 JavaScript 每 30 行。

标签: asp.net vb.net


【解决方案1】:

事实证明我需要做两件事。 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 代码头中添加以下内容:

EnableEventValidation = "假"

现在一切正常,我添加了一个类似的“导出到 Excel”功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    相关资源
    最近更新 更多