【发布时间】:2015-10-20 23:50:41
【问题描述】:
我有一个名为“答案”的调查回复字符串数组。 string[] answers = new string[10] {"Y", "Y", "N", "Y", "N", "Y", "N", "Y", "N", "Y"};
我知道,如果我像这样为每个索引创建一个新字符串:string answer1 = answers[0]
我可以将 放在 HTML/ASPX 页面中,它会按应有的方式显示“Y”。但是,我的数组将有超过 50 个项目(尽管项目的数量不会改变)。
一种方法是创建 50 个变量并单独引用它们,但说实话..
这是我的<body> ;)(或者至少是我想要的样子。)
<body>
<div class="container">
<asp:Panel ID="pnlResults1" runat="server">
<div class="section z-depth-2 blue-grey lighten-5">
<table style="width: 25%" align="center">
<tr>
<td><b>Question 1:</b></td>
<td><%=answer1%></td>
</tr>
<tr>
<td><b>Question 2</b></td>
<td><%=answer2%></td>
</tr>
<tr>
<td><b>Question 3</b></td>
<td><%=question3%></td>
</tr>
<tr>
<td><b>Question 4</b></td>
<td><%=question4%></td>
</tr>
<tr>
<td><b>Question 5</b></td>
<td><%=question5%></td>
</tr>
<tr>
<td><b>Question 6</b></td>
<td><%=question6%></td>
</tr>
</table>
</div>
</asp:Panel>
</div>
..并且表格行数将继续计数。基本上,我想在一个表格中显示我的 50 多个调查回复,而不必为每个数组索引创建一个字符串。
我正在考虑为 html/aspx 中的答案创建文本框,并使用 foreach 循环遍历每个文本框并在迭代答案 [] 时添加答案,但我不确定如何迭代答案.
类似(伪代码)
int counter = 0;
foreach(Control c in Panel){
if(c is Text)
Text.setText(answers[counter]);
counter++;
}
任何帮助将不胜感激。谢谢!
【问题讨论】:
-
使用 asp:Repeater 代替 asp:Panel,并在后面的代码中设置中继器的数据源。