做個備忘,說明如何把GridView或其他WebControl輸出成html碼
以下文章轉錄自topcat姍舞之間的極度凝聚
如果我們想要在CodeFile中取得某控制項運作後將會送出的HTML內容可以透過以下的Function來處理。
    Public Function GetHTML(ByVal objWebControl As WebControl) As String
        Try
            '以 StringWriter 取得HTML
            Dim w As New System.IO.StringWriter
            Dim a As New HtmlTextWriter(w)
            objWebControl.RenderControl(a)
            Return w.ToString
        Catch ex As Exception
            Throw
        End Try
    End Function
    Public Function GetHTML(ByVal objHtmlControl As HtmlControl) As String
        Try
            '以 StringWriter 取得HTML
            Dim w As New System.IO.StringWriter
            Dim a As New HtmlTextWriter(w)
            objHtmlControl.RenderControl(a)
            Return w.ToString
        Catch ex As Exception
            Throw
        End Try
    End Function
如果想在CodeFile中取得GridView結果的HTML內容,首先會遇到這樣的錯誤訊息:
型別 ‘GridView’ 的控制項 ‘GridView1’ 必須置於有 runat=server 的表單標記之中。
這個問題,可以在您的CodeFile中加入以下這段來解決
    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
        '處理'GridView' 的控制項 'GridView' 必須置於有 runat=server 的表單標記之中
    End Sub
如果您的GridView沒有設定分頁,應該就可以正常的執行了。但是如果有設定分頁,很快的又遇到了另外一個問題!出現以下的錯誤訊息:
RegisterForEventValidation 只能在 Render(); 期間呼叫
這個問題,可以設定aspx原始檔中<%Page%>的以下兩個設定解決
EnableEventValidation = “false” AutoEventWireup=”true”
參考資料:
解決GridView使用RenderControl取得HTML出現的問題
在CodeFile中取得控制項將輸出Clinet端的HTML內容
使用 BasePage 來解決 GridView 執行 RenderControl 產生的錯誤 
謝謝你的幫助!