tags:

views:

554

answers:

4

I have any ASP.NET control. I want the HTML string how to do I get the HTML string of the control?

A: 

If you want the rendered html on the client, just use firebug and use its Inspect feature.

Gulzar
I want to render it on the server side, and then e-mail out in HTML e-mail.
David Basarab
Fine. I was thinking of a scenario where you dont have the source code of the control (like a dll).
Gulzar
+4  A: 

This appears to work.

public string RenderControlToHtml(Control ControlToRender)
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    System.IO.StringWriter stWriter = new System.IO.StringWriter(sb);
    System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(stWriter);
    ControlToRender.RenderControl(htmlWriter);
    return sb.ToString();
}
David Basarab
+2  A: 

If your control is a web user control, this is how you can get to the HTML it emits from another page or handler:

public void GetHtmlFromMySweetControl(HttpContext context)
{
    HttpRequest httpRequest = context.Request;
    HttpResponse httpResponse = context.Response;

    string foo = httpRequest["foo"];

    Page pageHolder = new Page();
    string path = "~/usercontrols/MySweetControl.ascx";
    MySweetControl ctrl = (MySweetControl)pageHolder.LoadControl(path);
    ctrl.BindProducts(foo);
    pageHolder.Controls.Add(ctrl);

    StringWriter sw = new StringWriter();
    context.Server.Execute(pageHolder, sw, false);
    httpResponse.Write(sw.ToString());
}
AndrewDotHay
A: 

Hi there, there's an error when i'm trying to render my Web User Control has many server control inside and it said that:

*Control 'ctl00_CheckBox1' of type 'CheckBox' must be placed inside a form tag with runat=server.*

While checkbox1 is my control in Web User Control

Is there any idea???

Ths so much

I'm just new in asp.net!!!

Start a new question.
Alex