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

Ema*_*een 2 vb.net asp.net stringbuilder rendercontrol string-conversion

我们尝试从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
Run Code Online (Sandbox Code Playgroud)

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

Control 'BodyPlaceholder_GridViewSummary' of type 'GridView' must be placed 
inside a form tag with runat=server.
Run Code Online (Sandbox Code Playgroud)

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

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

<form runat="server">
Run Code Online (Sandbox Code Playgroud)

现在我们得到这个错误:

A page can have only one server-side Form tag.
Run Code Online (Sandbox Code Playgroud)

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

这是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>      
Run Code Online (Sandbox Code Playgroud)

Van*_*dze 5

在页面中添加以下子,然后重试:

Public Overrides Sub VerifyRenderingInServerForm(control As Control)
    Return
End Sub
Run Code Online (Sandbox Code Playgroud)

编辑:

要关闭事件验证,只需将EnableEventValidation ="False"添加到aspx页面的指令中.例如

<%@ Page EnableEventValidation="False" %>
Run Code Online (Sandbox Code Playgroud)