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>      
5
  • Are you using a master page? Commented Nov 27, 2012 at 17:15
  • You'll probably find your missing form control in the master page. Maybe it's missing the runat="server" attribute Commented Nov 27, 2012 at 17:20
  • The master page did have it in there so I removed it from the page with the GridView and the original error message came back telling me to place the GridView in a form tag. The error is on this line: GridViewSummary.RenderControl(htmlTW) Commented Nov 27, 2012 at 17:25
  • Does it have the runat=server in the tag in the master version? If not, add it. Commented Nov 27, 2012 at 17:29
  • Yes, runat="server" is there. The line is: <form id="form1" runat="server" class="Header"> Commented Nov 27, 2012 at 17:35

1 Answer 1

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" %>
Sign up to request clarification or add additional context in comments.

2 Comments

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?
@VanoMaisuradze Thanks a lot.. i have same problem and try this solution..and its work for me..thanks a lot..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.