2

我们试图从GridView获取HTML并将其存储到字符串中,以便可以将字符串用作电子邮件的正文。将ASP.Net GridView HTML转换为字符串

到目前为止,我们已经在使用该编码的代码隐藏:

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. 

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

​​

现在我们得到这个错误:

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>  
+0

您是否正在使用母版页? – mcalex

+0

是的,我们正在那样做。 –

+0

您可能会在母版页中找到您缺少的表单控件。也许它缺少'runat =“server”'属性 – mcalex

回答

5

添加下面的子,然后再试一次:

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

编辑:

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

<%@ Page EnableEventValidation="False" %> 
+0

感谢Vano的编码。添加完后,现在显示此错误:只能在Render()期间调用RegisterForEventValidation;我怎么做? –

+0

看到我编辑的答案。 –

+0

感谢Vano。我把它放进去,它完美地工作。 :-) –