How to Get the HTML Output of a Usercontrol in .Net (C#)

How do I get the HTML output of a UserControl in .NET (C#)?

You can render the control using Control.RenderControl(HtmlTextWriter).

Feed StringWriter to the HtmlTextWriter.

Feed StringBuilder to the StringWriter.

Your generated string will be inside the StringBuilder object.

Here's a code example for this solution:

string html = String.Empty;
using (TextWriter myTextWriter = new StringWriter(new StringBuilder()))
{
using (HtmlTextWriter myWriter = new HtmlTextWriter(myTextWriter))
{
myControl.RenderControl(myWriter);
html = myTextWriter.ToString();
}
}

Get user control html in asp.net

i found out how to get around it

    Page p = new Page();
myControls.OrderInvoiceView oiv = (myControls.OrderInvoiceView)p.LoadControl("~/myControls/OrderInvoiceView.ascx");

instead of

myControls.OrderInvoiceView oiv = new myControls.OrderInvoiceView();

final result

        StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);

Page p = new Page();
myControls.OrderInvoiceView oiv = (myControls.OrderInvoiceView)p.LoadControl("~/myControls/OrderInvoiceView.ascx");

//myControls.OrderInvoiceView oiv = new myControls.OrderInvoiceView();
oiv.loadOrderInvoiceView(OID);
oiv.RenderControl(hw);

return sb.ToString();

Retrieve contents of HtmlGenericControl inside ASCX WebControl

OK, I got it working (I think...)

Output

Sample Image

ASPX Code behind

    public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}

ASPX markup

%@ Page EnableEventValidation="false" .....

User control code behind

    protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
var d = new QuestionsContext().GetQuestions();

this.repeater.DataSource = d;
this.repeater.DataBind();
}
}

protected void getContent_Click(object sender, EventArgs e)
{
var sb = new StringBuilder();

this.generic.RenderControl(new HtmlTextWriter(new StringWriter(sb)));

string s = sb.ToString();

this.Trace.Warn(Server.HtmlEncode(s));
this.message.Text = Server.HtmlEncode(s);
}

User control markup

<div runat="server" id="generic">
<asp:Repeater runat="server" ID="repeater" >
<ItemTemplate>
<%# Eval("QuestionText") %>
</ItemTemplate>
</asp:Repeater>
</div>

<br />
<asp:Button Text="Get content" ID="getContent" runat="server" OnClick="getContent_Click" />
<br />
<asp:Label ID="message" runat="server" />

Get the HTML rendered by ASP.NET control in Code behind

Does RenderControl() not work?

Create an instance of your control and then call RenderControl() on it. Of course this implies that your panel is in a UserControl

example from comments:

StringBuilder sb = new StringBuilder(); 
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
var html = sb.ToString();

How to get Generated Html form HtmlGenericControl

Trick told by @Bartdude worked like a charm...

For other peoples, solution goes in this way...

// create you generic controls
HtmlGenericControl mainDiv = new HtmlGenericControl("div");
// setting required attributes and properties
// adding more generic controls to it
// finally, get the html when its ready
StringBuilder generatedHtml = new StringBuilder();
using (var htmlStringWriter = new StringWriter(generatedHtml))
{
using(var htmlTextWriter = new HtmlTextWriter(htmlStringWriter))
{
mainDiv.RenderControl(htmlTextWriter);
output = generatedHtml.ToString();
}
}

Hope this helps for coming readers...:)

UserControl's RenderControl is asking for a form tag in (C# .NET)

Alternatively you could disable the ServerForm/Event-validation on the page that is rendering the control to a string.

The following example illustrates how to do this.

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string rawHtml = RenderUserControlToString();
}

private string RenderUserControlToString()
{
UserControl myControl = (UserControl)LoadControl("WebUserControl1.ascx");

using (TextWriter myTextWriter = new StringWriter())
using (HtmlTextWriter myWriter = new HtmlTextWriter(myTextWriter))
{
myControl.RenderControl(myWriter);

return myTextWriter.ToString();
}
}

public override void VerifyRenderingInServerForm(Control control)
{ /* Do nothing */ }

public override bool EnableEventValidation
{
get { return false; }
set { /* Do nothing */}
}
}

asp.net user control, getting htmlAnchor resolve to href=#

I had the same problem, here's how I could resolve it:

Original code

User control:

<a id="foo" runat="server">...</a>

Code behind:

foo.Attributes.Add("href", "#");

Output:

<a id="..." href="../Shared/Controls/#">...</a>

Updated code

User control:

<asp:HyperLink id="foo" runat="server">...</asp:HyperLink>

Code behind:

foo.Attributes.Add("href", "#");

Output:

<a id="..." href="#">...</a>

How do I get contents of UserControl from server using Jquery Ajax?

In the context of Page, you can load a control and render its output.

Control myControl = Page.LoadControl("myControl.ascx");
myControl.RenderControl(System.Web.UI.HtmlTextWriter);

But that just gets the html that the control would output to the page. This is fine if your control has no more events and just does something onload, but if you want to postback from it, then this will not work (as mentioned in the comments.)



Related Topics



Leave a reply



Submit