Have you ever needed to render your ASP.NET partial view as a string?  For example you have to replace a predefined string by the result of your partial view?  Well, I found a great function to render your partial view as a string in C#, and quickly converted it to VB.NET.  Thanks to Kevin for the C# code :)

Public Function RenderPartialToString(ByVal controlName As String, ByVal viewData As Object) As String

        Dim vd As New ViewDataDictionary(viewData)
        Dim vp As New ViewPage()
        vp.ViewData = vd
        Dim control = vp.LoadControl(controlName)

        vp.Controls.Add(control)

        Dim sb As New StringBuilder()
        Using sw As New StringWriter(sb)
            Using tw As New HtmlTextWriter(sw)
                vp.RenderControl(tw)
            End Using
        End Using

        Return sb.ToString()
    End Function

01/14/2010 - 09:55
.NET MVC, VB.NET