[英]How to display array from C# codebehind to ASPX page?
我有一個名為“答案”的調查問卷響應的字符串數組。
string[] answers = new string[10] {"Y", "Y", "N", "Y", "N", "Y", "N", "Y", "N", "Y"};
我知道如果我為每個索引創建一個新的字符串,像這樣:
string answer1 = answers[0]
我可以在HTML / ASPX頁中放入<%= answer1%>,它會按原樣顯示“ 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循環遍歷每個答案,並在遍歷Answers []的同時添加答案,但是我不確定如何遍歷答案。
類似於(偽代碼)
int counter = 0;
foreach(Control c in Panel){
if(c is Text)
Text.setText(answers[counter]);
counter++;
}
任何幫助,將不勝感激。 謝謝!
將其放入您的* .aspx文件:
<div class="container">
<div class="section z-depth-2 blue-grey lighten-5">
<asp:Repeater ID="rptResults1" runat="server">
<HeaderTemplate><table style="width: 25%" align="center"></HeaderTemplate>
<ItemTemplate>
<tr>
<td><strong>Question <%# Container.ItemIndex + 1 %>:</strong></td>
<td><%# Container.DataItem %></td>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</div>
</div>
然后將其放入您的代碼隱藏中:
string[] answers = new string[10] {"Y", "Y", "N", "Y", "N", "Y", "N", "Y", "N", "Y"};
rptResults1.DataSource = answers;
rptResults1.DataBind();
如果確實需要,可以將其放入標記中:
<div class="container">
<div class="section z-depth-2 blue-grey lighten-5">
<table style="width: 25%" align="center">
<%
string[] answers = new string[10] {"Y", "Y", "N", "Y", "N", "Y", "N", "Y", "N", "Y"};
string rowTemplate = "<tr><td><b>Question {0}:</b></td><td>{1}</td></tr>\n";
for (int i = 0; i<answers.Length;i++)
{
Response.Write(string.Format(rowTemplate, i+1, answers[i]));
}
%>
</table>
</div>
</div>
不過,我不建議第二種選擇。 如果您喜歡,可以嘗試了解Razor語法 ,甚至可以使用ASP.Net MVC而不是Web Forms。
我會考慮使用中繼器。 我不會在頁面加載或任何東西上綁定它,但這是一個示例。 將數據和綁定移動到用於獲取數據的任何事件。
<asp:Repeater runat="server" ID="repeat" OnItemDataBound="repeat_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lbl" runat="server" />
</ItemTemplate>
</asp:Repeater>
public partial class _Default : Page
{
int count = 0;
protected void Page_Load(object sender, EventArgs e)
{
string[] answers = new string[10] { "Y", "Y", "N", "Y", "N", "Y", "N", "Y", "N", "Y" };
repeat.DataSource = answers;
repeat.DataBind();
}
protected void repeat_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
count++;
var lbl = e.Item.FindControl("lbl") as Label;
lbl.Text = string.Format("Question {0} <br />{1}<br />",count.ToString(), e.Item.DataItem.ToString());
}
}
count變量是在網頁中聲明的簡單整數。
我將runat =“ server”添加到表中,然后可以在C#中使用問題號和答案在循環中添加每個表行。
<body>
<div class="container">
<asp:Panel ID="pnlResults1" runat="server">
<div class="section z-depth-2 blue-grey lighten-5">
<table runat="server" ID="tblResults" style="width: 25%" align="center">
</table>
</div>
</asp:Panel>
</div>
foreach (var answer in answers)
addRow(questionNumber, answer)
private void addRow(questionNumber, answer)
{
//build your table row and add to table
}
我將創建一個包含答案的字符串列表,而不是為每個答案創建一個新變量。
List<String> lstOfAnswers = new List<String>();
lstOfAnswers.Add(...);
或者,您也可以考慮使用“字典”,其中“鍵”是問題的編號,而“值”是答案。
Dictionary<int, string> dictOfAnswers = new Dictionary<int, string>();
dictOfAnswers.Add(Key,Value);
在您的Page(html)內部,您可以使用foreach命令像這樣遍歷列表或字典
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.