使用响应中的WebControl
问题描述:
同花顺,我想使用某种逐步呈现在三夏,这里有一个例子(没有工作像预想的那样)使用响应中的WebControl
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);
}
}
这应该在每次刷新渲染。也许这是一个可怕的技术,但我爱这个建议。
答
您的方法可能无法按预期工作。如果你想逐步渲染(我不是问为什么),你应该解决类似AJAX。
有太多的参与(网络,客户端和服务器端的缓冲,在浏览器中呈现非常依赖于当前等其他元素的结束标记),您可以使用一种方法,只需暂停间歇地生成内容。
如果您想要在服务器上显示类似服务器进程的进程,请让其他后台线程完成该任务,让客户端每隔X秒轮询一次更新。 JSON和AJAX是您的朋友,您可以向用户展示缓慢增长的页面/内容。
这样做的效果是,您可以先渲染整个页面(即包含html结束标记的有效HTML),然后更新HTML页面中间的部分。
答
要做到这一点你试图,你需要呈现控制(可能).ToString()调用。然后,在调用response.Flush()之前调用response.Write(yourString)。
艾贝尔提到的方法是,国际海事组织,一个更好的。
此博客文章指出您需要两次调用response.Flush ...这可能与您正在做的事情一起工作。当我需要做类似的事情时,我使用了Abel的方法。