C#中,如何foreach循环过程中产生新的HTML DIV? [英] C#, how to generate new html Div during foreach loop?
问题描述
依靠结果我得到了像3的结果数量,我想每个结果以标签文本并投入新的div每个新行分配。我该怎么办?
Depend of number of results I got like 3 results and I want to assign each result to label text and put to new Div each new line. How I do that?
收集和LT; PSObject>输出= pipeline.Invoke();
是我储存的结果。
下面的示例C#codeS,
Here a sample C# codes,
foreach(PSObject psObject in output)
{
Label ipLabel = new Label();
ipLabel.Text = psObject + "\r\n";
div1.Controls.Add(ipLabel);
}
下面一个asp.net codeS,
Here a asp.net codes,
<div id="div1" runat="server"></div>
我得到了正确的标签结果,但是这一切在1格行而不是每一个div的新行。显示
I got the right Label results but it all displayed in 1 div line instead of every div new line .
推荐答案
在理想情况下,标记应的操作使用数据绑定在你的的.aspx
文件,而不是在做你的codebehind,但无论如何:
Ideally, markup manipulation should be done using data-binding within your .aspx
file rather than in your codebehind, but anyway:
foreach(PSObject psObject in output) {
HtmlGenericControl div = new HtmlGenericControl("div");
div.Controls.Add( new Label() { Text = psObject + "\r\n"; } );
div1.Controls.Add( div );
}
这是我会怎么做的aspx文件:
This is how I would do it in the aspx file:
<div id="div1">
<% foreach(PSObject psObject in output) { %>
<div>
<%: psObject %>
</div>
<% } //foreach %>
</div>
请注意使用&LT;%:富%&GT;
相当于&LT;%= Server.HtmlEn code(富)%方式&gt;
这篇关于C#中,如何foreach循环过程中产生新的HTML DIV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!