I’d like to use some sort of stepwise rendering in a webcontrol, here is an example (not working as supposed)
public class Price : WebControl
{
protected override void Render(HtmlTextWriter output)
{
HttpResponseBase response = new HttpResponseWrapper(new HttpResponse(output));
//Panel basePanel = new Panel() { ID = "basePanel" };
//Controls.Add(basePanel);
//var loaderImage = LoaderAnimation();
//Controls.Add(loaderImage);
System.Threading.Thread.Sleep(500);
Controls.Add(new LiteralControl("aaa "));
response.Flush();
System.Threading.Thread.Sleep(500);
Controls.Add(new LiteralControl("bbb "));
response.Flush();
System.Threading.Thread.Sleep(500);
Controls.Add(new LiteralControl("ccc "));
base.RenderContents(output);
}
}
This should render at each flush. Maybe this is a terrible technique but Id love some advice in this.
Your approach probably won’t work as expected. If you want step-wise rendering (I’m not asking why), you should resolve to something like AJAX.
There’s too much involved (the network, buffering on client and server-side, rendering in the browser which is very dependent on end-tags of other elements than the current etc) that you can use an approach where you simply pause generation of content intermittently.
If you want to show something like the progress of a longer-taking process on the server, let another background thread do the job and let the client poll each X seconds for updates. JSON and AJAX are your friend there and you can show a slowly growing page/content to the user.
This has the added effect that you can render a whole page first (i.e. valid HTML including html end-tag) and then update parts in the middle of the HTML page.