Question

We are trying to take the HTML from a GridView and store it into a String so that string can be used a a body of an email.

So far we have used this coding in the code-behind:

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

When the application is running this error is displayed:

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

so I placed a form tag at this location in the markup:

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

<form runat="server">

Now we get this error:

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

There are no other form tags in anywhere else in the markup.

This is the markup for the 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>      
Was it helpful?

Solution

Add following sub in your page and try again:

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

Edit:

To turn off event validation, just add EnableEventValidation="False" into your aspx page's directive. For example

<%@ Page EnableEventValidation="False" %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top