1

I have a string array of survey responses called "answers".
string[] answers = new string[10] {"Y", "Y", "N", "Y", "N", "Y", "N", "Y", "N", "Y"};

I know that if I create a new string for each index like so:
string answer1 = answers[0]

I can put <%=answer1%> in the HTML/ASPX page and it will display "Y" as it should. However, my array will have upwards of 50 items (although the number of items won't change).

One way to do it is creating 50 variables and referencing them individually but let's be honest..

Here's my <body> ;) (or what I want it to look like, at least.)

<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>

..and the table rows will keep counting. Basically, I want to display my 50+ survey responses in a table without having to create a string for each array index.

I was thinking of creating text boxes for the answers in html/aspx and using a foreach loop to go through each one and adding answers while iterating through answers[], but I'm not sure how I would iterate through the answers.

Something like (pseudocode)

int counter = 0;
foreach(Control c in Panel){
     if(c is Text) 
        Text.setText(answers[counter]);
        counter++;
}

Any help would be appreciated. Thanks!

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Vincent Song
  • 25
  • 1
  • 1
  • 6

4 Answers4

5

Put this into your *.aspx file:

<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>

Then put this into your code-behind:

string[] answers = new string[10] {"Y", "Y", "N", "Y", "N", "Y", "N", "Y", "N", "Y"};
rptResults1.DataSource = answers;
rptResults1.DataBind();

If you really want to, you can put this into your markup:

<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>

I don't recommend this second option, though. If it appeals to you, try learning about the Razor syntax instead, or even using ASP.Net MVC instead of Web Forms.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Thanks, @JoelCoehoorn I copy/pasted and it works, but I'd like to understand how the `Question <%# Container.ItemIndex + 1 %>: <%# Container.DataItem %>` and `string[] answers = new string[10] {"Y", "Y", "N", "Y", "N", "Y", "N", "Y", "N", "Y"}; rptResults1.DataSource = answers; rptResults1.DataBind();` works together. For example, what if I had an array of questions? How would I add that in along with the corresponding answers? Again, thank you for your answer. I'm still trying to figure it out but at least I know where to start. :) – Vincent Song Jul 29 '15 at 20:45
  • You can only have one data source for a control. If you have two arrays, one for questions and and for answers, you have to change that so the data for each record is in one place. Paired arrays like that are an anti-pattern anyway. – Joel Coehoorn Jul 29 '15 at 20:49
2

I'd consider using a repeater. I wouldn't bind it on page load or anything, but here is an example. Move the data and the binding to whatever event is used to get the data.

<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());
    }
}

the count variable is a simple integer declared in the web page.

Adam Heeg
  • 1,704
  • 1
  • 14
  • 34
1

I would add runat="server" to the table, then you can add each table row in a loop in C# with the question number and the answer.

<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
}
Seano666
  • 2,238
  • 1
  • 15
  • 14
1

I would create a List of Strings that will contain the answers, instead of creating a new variable for each answer.

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

Or you can think about a Dictionary where the Key is the number of the question and the Value is the answer.

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

And inside your Page(html), you can iterate through the List or Dictionary using the foreach command like this

Hope it helps.

Community
  • 1
  • 1
Maturano
  • 951
  • 4
  • 15
  • 42