вівторок, 30 листопада 2010 р.

Render user control to a string

Simple render:

var c = LoadControl("uc.ascx");
using(var sw = new System.IO.StringWriter())
using(var tw = new HtmlTextWriter(sw))
{
c.RenderControl(tw);
Controls.Add(new LiteralControl(sw.ToString()));
}


Execute and render:

var c = LoadControl("uc.ascx");
var page = new Page();
page.Controls.Add(c);

using(var sw = new System.IO.StringWriter())
using(var tw = new HtmlTextWriter(sw))
{
Server.Execute(page, tw, false);
Controls.Add(new LiteralControl(sw.ToString()));
}

Немає коментарів:

Дописати коментар