2

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>      
Emad-ud-deen
  • 4,734
  • 21
  • 87
  • 152

1 Answers1

5

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" %>
Vano Maisuradze
  • 5,829
  • 6
  • 45
  • 73
  • Thanks Vano for the coding. After I added it, this error is now displayed: RegisterForEventValidation can only be called during Render(); How do I do that? – Emad-ud-deen Nov 27 '12 at 18:13
  • @VanoMaisuradze Thanks a lot.. i have same problem and try this solution..and its work for me..thanks a lot.. – Harshit Tailor Oct 14 '13 at 04:48