Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Render a user control to a string?

Render a user control to a string?

Scheduled Pinned Locked Moved ASP.NET
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ChrisWFL
    wrote on last edited by
    #1

    I have an ASCX user control that I want to render to a string instead of direct to the browser. I've tried something like: MyControls.footer ftGen = new MyControls.footer(); System.Text.StringBuilder sb = new StringBuilder(); System.IO.StringWriter twriter = new System.IO.StringWriter(sb); HtmlTextWriter writer = new HtmlTextWriter(twriter); ftGen.RenderControl(writer); return sb.ToString(); but am not having any luck. Anybody have ideas? :confused:

    M 1 Reply Last reply
    0
    • C ChrisWFL

      I have an ASCX user control that I want to render to a string instead of direct to the browser. I've tried something like: MyControls.footer ftGen = new MyControls.footer(); System.Text.StringBuilder sb = new StringBuilder(); System.IO.StringWriter twriter = new System.IO.StringWriter(sb); HtmlTextWriter writer = new HtmlTextWriter(twriter); ftGen.RenderControl(writer); return sb.ToString(); but am not having any luck. Anybody have ideas? :confused:

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      Hi Chris. I tried this sort of thing once and if I remember right, the error was that the user control wasn't being instanciated in a <form runat='server'> server form. Is that what's happening for you? What benefit are you looking for in returning the rendered control as a string? In my case I was attempting to ultimately have the .ascx rendered in the web page after inspecting the string. I ended up just adding the usercontrol to the custom control's Controls collection and letting normal ASP.NET rendering do its thing. Sorry I can't be more helpful here.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups