Here’s how to extract the Html code for a web control:
public static string GetHtmlForControl(Control control) { StringWriter textwriter = new StringWriter(); HtmlTextWriter writer = new HtmlTextWriter(textwriter); control.DataBind(); control.RenderControl(writer); string html = textwriter.GetStringBuilder().ToString(); return html; }
Tip: If you’re having problems with the generated Html being incomplete, then maybe you’re calling the function above in the wrong time? I’ve been having some problems with databound EPiServer controls until we discovered that we were doing this too early. When we started doing it in the PreRenderComplete event of the page, then it started working:
protected void Page_PreRenderComplete(object sender, EventArgs e) { string html = FormHelper.GetHtmlForControl(Butikshuvud); }