[英]Is there any asp.net component to write instead of adding
So far I know we have LiteralControl
and PlaceHolder
that we can use to add controls to ASP.NET page dynamically at run time. 到目前为止,我知道我们拥有
LiteralControl
和PlaceHolder
,可用于在运行时将控件动态添加到ASP.NET页面。
To add html code to LiteralControl
we use 要将HTML代码添加到
LiteralControl
我们使用
LiteralControl.Text = "Some HTML/Text"
and for PlaceHolder
we use 对于
PlaceHolder
我们使用
PlaceHolder.Controls.Add(new LiteralControl("Some HTML/Text"))
I'm looking for a component that I can write to it like what I do with Response.Write(...)
but I need it to write in a designated place in page. 我正在寻找一个可以像对
Response.Write(...)
一样进行写入的组件,但是我需要它在页面的指定位置进行写入。 I need to call this many times to send small chunks of html code to output to save memory. 我需要调用多次以发送一小段html代码以输出以节省内存。
So the component usage will be something like this: 因此,组件的用法将如下所示:
in aspx page I'll put: 在aspx页面中,我将输入:
<body>
...
<div>
<asp:componentName ID="SomeComp" runat="server" />
</div>
...
</body>
And in my code behind, I'll use this ( Imagine a big number for SetCount ): 在后面的代码中,我将使用此代码( 想象一下SetCount有很多数字 ):
for (int i;i<SetCount;i++){
SomeComp.Write("Some Text/HTML Code");
}
While(Read.Read()){
SomeComp.Write("Some More Text/HTML Code");
}
FYI Adding to strings in DOT.NET
is very slow so LiteralControl
is not a good choice. 仅供参考在
DOT.NET
添加到字符串非常慢,因此LiteralControl
不是一个不错的选择。
Creating an StringBuilder and using 创建一个StringBuilder并使用
LiteralControl.Text = StringBuilder.ToString()
is not an option because it keeps all string in memory until you assign it to control and dispose it. 这不是一个选项,因为它将所有字符串保留在内存中,直到您将其分配给控件并处置为止。
PlaceHolder.Controls.Add(new LiteralControl("Some HTML/Text"))
is not an option because it creates one LiteralControl for each chunk of html I add to it and again it uses lots of memory that for me is limited. 这不是一个选择,因为它为我添加到其中的每个HTML块创建了一个LiteralControl,并且它再次使用了很多对我来说有限的内存。
My Intranet Website has about 500 Calls for same page in a second and it generates a huge spike in memory use that makes IIS stop responding to requests for other applications. 我的Intranet网站每秒可访问约500个同一页面,并且会导致内存使用量激增,从而使IIS停止响应其他应用程序的请求。
So Far I'm not aware of such internal ASP.NET
component. 到目前为止,我还不了解这种内部
ASP.NET
组件。
For this matter I suggest 3 solution: 对于这个问题,我建议3解决方案:
Response.Write(...)
and write everything with that. Response.Write(...)
并用它编写所有内容。 if you are using Master Page or have some other components. One more thing to consider is if your data is no changing a lot you can cache your page to reduce the hits on IIS too. 需要考虑的另一件事是,如果您的数据没有太大变化,则可以缓存页面以减少对IIS的访问。
for the problem of creating a single LiteralControl object for each html element, you can use Html32TextWriter to render all of your html elements first, and then use a LiteralControl to put it on the page : 对于为每个html元素创建单个LiteralControl对象的问题,可以使用Html32TextWriter首先呈现所有html元素,然后使用LiteralControl将其放在页面上:
var sb = new StringBuilder();
var writer = new StringWriter(sb);
var html = new Html32TextWriter(writer);
html.RenderBeginTag(HtmlTextWriterTag.P);
html.WriteEncodedText("paragraph1");
html.RenderEndTag();
...
...
html.Flush();
var outputHtml = sb.ToString();
placeHolder.Controls.Add(new LiteralControl(outputHtml));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.