Render Control Help?

Discussion in 'ASP .Net' started by =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?=, Aug 6, 2007.

  1. 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 BuildPropertyDetialsInfo() As String
    Dim propertyInfoControl As New StringBuilder
    Dim ms As New MemoryStream
    Dim propertyInfoUserControl As New UserControl
    Dim propertyInfoStringWriter As New StringWriter(propertyInfoControl)
    Dim propertyInfoHTMLWriter As New HtmlTextWriter(New StreamWriter(ms))
    Dim html As String = String.Empty

    Try
    propertyInfoHTMLWriter.BeginRender()
    propertyInfoUserControl.LoadControl(renderControl)
    propertyInfoUserControl.RenderControl(propertyInfoHTMLWriter)
    propertyInfoHTMLWriter.EndRender()
    propertyInfoHTMLWriter.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
    =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?=, Aug 6, 2007
    #1
    1. Advertising

  2. A UserControl is a Templated Control. If you want a class that doesn't
    render a UI, but writes HTML, write a class that uses an HtmlTextWriter to
    write to a stream or to a string. An HtmlTextWriter is initialized with a
    TextWriter-derived class, which includes both StreamWriter and StringWriter,
    so you can use an HtmlTextWriter to build an HTML string as well as writing
    to a stream.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP

    Printing Components, Email Components,
    FTP Client Classes, Enhanced Data Controls, much more.
    DSI PrintManager, Miradyne Component Libraries:
    http://www.miradyne.net

    "Daniel Di Vita" <> wrote in message
    news:...
    >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 BuildPropertyDetialsInfo() As String
    > Dim propertyInfoControl As New StringBuilder
    > Dim ms As New MemoryStream
    > Dim propertyInfoUserControl As New UserControl
    > Dim propertyInfoStringWriter As New
    > StringWriter(propertyInfoControl)
    > Dim propertyInfoHTMLWriter As New HtmlTextWriter(New
    > StreamWriter(ms))
    > Dim html As String = String.Empty
    >
    > Try
    > propertyInfoHTMLWriter.BeginRender()
    > propertyInfoUserControl.LoadControl(renderControl)
    > propertyInfoUserControl.RenderControl(propertyInfoHTMLWriter)
    > propertyInfoHTMLWriter.EndRender()
    > propertyInfoHTMLWriter.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
    >
    >
    >
    Kevin Spencer, Aug 7, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    707
  2. aymer
    Replies:
    0
    Views:
    453
    aymer
    Aug 30, 2003
  3. Lau Lei Cheong

    Page.Render do not render complete page

    Lau Lei Cheong, May 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,047
    Lau Lei Cheong
    May 15, 2004
  4. Replies:
    1
    Views:
    358
    Teemu Keiski
    Jul 2, 2003
  5. Glenn Gillen
    Replies:
    0
    Views:
    262
    Glenn Gillen
    Nov 17, 2006
Loading...

Share This Page