将ASP.Net GridView HTML转换为字符串

时间:2012-11-27 17:13:24

标签: asp.net vb.net stringbuilder string-conversion rendercontrol

我们正在尝试从GridView中获取HTML并将其存储到String中,以便该字符串可以用作电子邮件的正文。

到目前为止,我们在代码隐藏中使用了这种编码:

Protected Sub EmailStudentList()

    ' Get the rendered HTML.
    '-----------------------
    Dim SB As New StringBuilder()
    Dim SW As New StringWriter(SB)
    Dim htmlTW As New HtmlTextWriter(SW)

    GridViewSummary.RenderControl(htmlTW)

    ' Get the HTML into a string.
    ' This will be used in the body of the email report.
    '---------------------------------------------------
    Dim dataGridHTML As String = SB.ToString()

    MsgBox(Server.HtmlEncode(dataGridHTML))
End Sub

当应用程序运行时,会显示以下错误:

Control 'BodyPlaceholder_GridViewSummary' of type 'GridView' must be placed 
inside a form tag with runat=server.

所以我在标记中的这个位置放置了一个表单标记:

<asp:Content
    ID="ContentBody"
    ContentPlaceHolderID="BodyPlaceholder"
    runat="server">

<form runat="server">

现在我们收到此错误:

A page can have only one server-side Form tag.

标记中的其他任何位置都没有其他表单标记。

这是GridView的标记:

<asp:GridView 
    ID="GridViewSummary" 
    runat="server" 
    AllowPaging="True" 
    AllowSorting="True" 
    AutoGenerateColumns="False">

    <Columns>
        <asp:BoundField DataField="Surname" HeaderText="Last Name" SortExpression="Surname" />
        <asp:BoundField DataField="Forename" HeaderText="First Name" SortExpression="Forename" />
        <asp:BoundField DataField="ParentName" HeaderText="Parents" SortExpression="ParentName" />
    </Columns>

</asp:GridView>      

1 个答案:

答案 0 :(得分:5)

在您的信息页中添加以下子信息,然后重试:

Public Overrides Sub VerifyRenderingInServerForm(control As Control)
    Return
End Sub

编辑:

要关闭事件验证,只需在您的aspx页面指令中添加EnableEventValidation =“False”即可。例如

<%@ Page EnableEventValidation="False" %>