【问题标题】:How to display array from C# codebehind to ASPX page?如何显示从 C# 代码隐藏到 ASPX 页面的数组?
【发布时间】: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,并在后面的代码中设置中继器的数据源。

标签: c# asp.net arrays loops


【解决方案1】:

把它放到你的 *.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 syntax,或者甚至使用 ASP.Net MVC 而不是 Web 表单。

【讨论】:

  • 谢谢,@JoelCoehoorn 我复制/粘贴了它,但我想了解&lt;td&gt;&lt;strong&gt;Question &lt;%# Container.ItemIndex + 1 %&gt;:&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;%# Container.DataItem %&gt;&lt;/td&gt;string[] answers = new string[10] {"Y", "Y", "N", "Y", "N", "Y", "N", "Y", "N", "Y"}; rptResults1.DataSource = answers; rptResults1.DataBind(); 是如何协同工作的。例如,如果我有一系列问题怎么办?我将如何将其与相应的答案一起添加?再次感谢您的回答。我仍在试图弄清楚,但至少我知道从哪里开始。 :)
  • 一个控件只能有一个数据源。如果您有两个数组,一个用于问题和一个答案,您必须更改它,以便每条记录的数据都在一个位置。无论如何,这样的配对数组都是一种反模式。
【解决方案2】:

我会考虑使用中继器。我不会在页面加载或任何东西上绑定它,但这里有一个例子。将数据和绑定移动到用于获取数据的任何事件。

<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 变量是网页中声明的简单整数。

【讨论】:

    【解决方案3】:

    我会将 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
    }
    

    【讨论】:

      【解决方案4】:

      我将创建一个包含答案的字符串列表,而不是为每个答案创建一个新变量。

      List<String> lstOfAnswers = new List<String>();
      lstOfAnswers.Add(...);
      

      或者您可以考虑一个字典,其中键是问题的编号,值是答案。

      Dictionary<int, string> dictOfAnswers = new Dictionary<int, string>();
      dictOfAnswers.Add(Key,Value);
      

      在您的 Page(html) 中,您可以使用 foreach 命令 like this 遍历列表或字典

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-27
        • 1970-01-01
        • 1970-01-01
        • 2011-11-12
        • 1970-01-01
        • 2012-06-25
        相关资源
        最近更新 更多