I posted this a week ago, but did not get a respone yet.
I need to render a user control as a HTML string. I do not want the control
to appear on the page, however. Here is what I am doing…
I have an HTML template that will ultimately be sent via e-mail. Inside the
template I have IDs setup for each section that will be replaced with dynamic
data. In one section I need to display a specific kind of data. I already
have a user control that generates what I need. I need to get that control to
fire and then get rendered as HTML to be placed in the appropriate section of
the fore mentioned HTML template. This is the code I have so far:
Public Function BuildPropertyDe tialsInfo() As String
Dim propertyInfoCon trol As New StringBuilder
Dim ms As New MemoryStream
Dim propertyInfoUse rControl As New UserControl
Dim propertyInfoStr ingWriter As New StringWriter(pr opertyInfoContr ol)
Dim propertyInfoHTM LWriter As New HtmlTextWriter( New StreamWriter(ms ))
Dim html As String = String.Empty
Try
propertyInfoHTM LWriter.BeginRe nder()
propertyInfoUse rControl.LoadCo ntrol(renderCon trol)
propertyInfoUse rControl.Render Control(propert yInfoHTMLWriter )
propertyInfoHTM LWriter.EndRend er()
propertyInfoHTM LWriter.Flush()
ms.Position = 0
Dim sr As New StreamReader(ms )
html = sr.ReadToEnd
Catch ex As Exception
End Try
Return html
I donl’t get anything back. I am assuming that I need to add the control to
the collection, but I don’t know how I would do this since I am not working
on a web form. I am inside a class that doens’t have access to a form page.
Any thoughts?
Daniel