簡體   English   中英

將ASP.Net GridView HTML轉換為字符串

[英]Converting ASP.Net GridView HTML into a string

我們正在嘗試從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

當應用程序運行時,顯示此錯誤:

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

所以我在標記中的這個位置放置了一個表單標簽:

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

<form 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>      

在頁面中添加以下子項,然后重試:

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

編輯:

要關閉事件驗證,只需將EnableEventValidation =“ False”添加到aspx頁面的指令中。 例如

<%@ Page EnableEventValidation="False" %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM