在CodeFile中取得控制項將輸出Clinet端的HTML內容

在CodeFile中取得控制項將輸出Clinet端的HTML內容

如果我們想要在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

 

小喵把這一段內容放在PageBase裡面,讓所有頁面可以簡單的呼叫,PageBase的使用方式請看以下這篇:

ASP.NET中如何每一頁呼叫公用Function(PageBase)


以上這個方式,再使用GridView的時候會發生狀況,解決方式請參考以下這篇:

解決GridView使用RenderControl取得HTML出現的問題

 

 


以下是簽名:


Microsoft MVP
Visual Studio and Development Technologies
(2005~2019/6) 
topcat
Blog:http://www.dotblogs.com.tw/topcat