简体   繁体   English

如何使用for循环将选定单选按钮的结果显示到一个文本框中?

[英]How to display results from selected radio buttons into one textbox using a for loop?

I am making a html form and when the user submits the form an aspx page will load to display the results of the form. 我正在制作一个html表单,当用户提交表单时,将加载一个aspx页面以显示表单的结果。 I have 20 questions total with 5 radio buttons each. 我总共有20个问题,每个问题有5个单选按钮。 For some reason when I try to retrieve the selected radio buttons thru a for loop, it only displays one answer. 由于某些原因,当我尝试通过for循环检索选定的单选按钮时,它仅显示一个答案。 What am I doing wrong? 我究竟做错了什么?

I have tried changing the values in the for loop but it just seems to not be displaying correctly. 我尝试过更改for循环中的值,但似乎无法正确显示。

Here's the code for the aspx.cs 这是aspx.cs的代码

public void DisplayResults(double[] results)
    {
        try
        {  
            int q = 1;
            for (int i = 0; i <= 19; i++)
            {
                if (i == 0)
                {
                    txtResults.Text = "Course Results";
                    if (results[i] == 1)
                    {
                        txtResults.Text += "Q" + q + ": Strongly Disagree";
                    }
                    else if (results[i] == 2)
                    {
                        txtResults.Text += "Q" + q + ": Disagree";
                    }
                    else if (results[i] == 3)
                    {
                        txtResults.Text += "Q" + q + ": Neutral";
                    }
                    else if (results[i] == 4)
                    {
                        txtResults.Text += "Q" + q + ": Agree";
                    }
                    else if (results[i] == 5)
                    {
                        txtResults.Text += "Q" + q + ": Strongly Agree ZERO";
                    }
                }
                else if (i == 11)
                {
                    txtResults.Text = "Professor Results";
                    if (results[i] == 1)
                    {
                        txtResults.Text += "Q" + q + ": Strongly Disagree";
                    }
                    else if (results[i] == 2)
                    {
                        txtResults.Text += "Q" + q + ": Disagree";
                    }
                    else if (results[i] == 3)
                    {
                        txtResults.Text += "Q" + q + ": Neutral";
                    }
                    else if (results[i] == 4)
                    {
                        txtResults.Text += "Q" + q + ": Agree";
                    }
                    else if (results[i] == 5)
                    {
                        txtResults.Text += "Q" + q + ": Strongly Agree ELEVEN";
                    }
                }
                else
                {
                    if (results[i] == 1)
                    {
                        txtResults.Text += "Q" + q + ": Strongly Disagree";
                    }
                    else if (results[i] == 2)
                    {
                        txtResults.Text += "Q" + q + ": Disagree";
                    }
                    else if (results[i] == 3)
                    {
                        txtResults.Text += "Q" + q + ": Neutral";
                    }
                    else if (results[i] == 4)
                    {
                        txtResults.Text += "Q" + q + ": Agree";
                    }
                    else if (results[i] == 5)
                    {
                        txtResults.Text += "Q" + q + ": Strongly Agree ELSE";

                    }
                }
                if (txtResults.Text == "")
                {
                    throw new Exception();
                }
            }
        }
        catch (Exception e)
        {
            Console.Write(e.Message, "Error");
        }
    }

Here's the code for aspx 这是aspx的代码

        <fieldset>
            <legend>Feedback Results</legend>
            <asp:TextBox ID="txtResults" runat="server" TextMode="MultiLine" Width="100%" Height="500px"></asp:TextBox>
        </fieldset>

Here's my html code 这是我的html代码

<h3>Please answer these questions to the best of your knowledge:</h3>
        <fieldset>
            <legend>Course Content (Organization, Clarity of Expectations/Directions, Balance/Appropriateness)</legend>
            1. The course (or section) presented skills in a helpful sequence
            <br />
            <input type="radio" name="q1" value="5" />strongly agree
            <input type="radio" name="q1" value="4" />agree
            <input type="radio" name="q1" value="3" />neutral
            <input type="radio" name="q1" value="2" />disagree
            <input type="radio" name="q1" value="1" />strongly disagree
            <br />
            <br />
            2. The course (or section) provided an appropriate balance between instruction and practice
            <br />
            <input type="radio" name="q2" value="5" />strongly agree
            <input type="radio" name="q2" value="4" />agree
            <input type="radio" name="q2" value="3" />neutral
            <input type="radio" name="q2" value="2" />disagree
            <input type="radio" name="q2" value="1" />strongly disagree
            <br />
            <br />
            3. The course (or section) was appropriate for the stated level of the class
            <br />
            <input type="radio" name="q3" value="5" />strongly agree
            <input type="radio" name="q3" value="4" />agree
            <input type="radio" name="q3" value="3" />neutral
            <input type="radio" name="q3" value="2" />disagree
            <input type="radio" name="q3" value="1" />strongly disagree
            <br />
            <br />
            4. The course (or section) was organized in a way that helped me learn
            <br />
            <input type="radio" name="q4" value="5" />strongly agree
            <input type="radio" name="q4" value="4" />agree
            <input type="radio" name="q4" value="3" />neutral
            <input type="radio" name="q4" value="2" />disagree
            <input type="radio" name="q4" value="1" />strongly disagree
            <br />
            <br />
            5. The lab helped to complement the lectures
            <br />
            <input type="radio" name="q5" value="5" />strongly agree
            <input type="radio" name="q5" value="4" />agree
            <input type="radio" name="q5" value="3" />neutral
            <input type="radio" name="q5" value="2" />disagree
            <input type="radio" name="q5" value="1" />strongly disagree
            <br />
            <br />
            6. The course (or section) provided a mixture of explanation and practice
            <br />
            <input type="radio" name="q6" value="5" />strongly agree
            <input type="radio" name="q6" value="4" />agree
            <input type="radio" name="q6" value="3" />neutral
            <input type="radio" name="q6" value="2" />disagree
            <input type="radio" name="q6" value="1" />strongly disagree
            <br />
            <br />
            7. The course (or section) was effectively organized
            <br />
            <input type="radio" name="q7" value="5" />strongly agree
            <input type="radio" name="q7" value="4" />agree
            <input type="radio" name="q7" value="3" />neutral
            <input type="radio" name="q7" value="2" />disagree
            <input type="radio" name="q7" value="1" />strongly disagree
            <br />
            <br />
            8. The course (or section) assignments and lectures usefully complemented each other
            <br />
            <input type="radio" name="q8" value="5" />strongly agree
            <input type="radio" name="q8" value="4" />agree
            <input type="radio" name="q8" value="3" />neutral
            <input type="radio" name="q8" value="2" />disagree
            <input type="radio" name="q8" value="1" />strongly disagree
            <br />
            <br />
            9. The course (or section) instructions (including, manuals, handouts, etc.) were clear
            <br />
            <input type="radio" name="q9" value="5" />strongly agree
            <input type="radio" name="q9" value="4" />agree
            <input type="radio" name="q9" value="3" />neutral
            <input type="radio" name="q9" value="2" />disagree
            <input type="radio" name="q9" value="1" />strongly disagree
            <br />
            <br />
            10. The course (or section) work helped me understand concepts more clearly
            <br />
            <input type="radio" name="q10" value="5" />strongly agree
            <input type="radio" name="q10" value="4" />agree
            <input type="radio" name="q10" value="3" />neutral
            <input type="radio" name="q10" value="2" />disagree
            <input type="radio" name="q10" value="1" />strongly disagree
            <br />
            <br />
            11. Instructions for course (or section) materials (including manuals, handouts, etc.) were clear
            <br />
            <input type="radio" name="q11" value="5" />strongly agree
            <input type="radio" name="q11" value="4" />agree
            <input type="radio" name="q11" value="3" />neutral
            <input type="radio" name="q11" value="2" />disagree
            <input type="radio" name="q11" value="1" />strongly disagree
            <br />
            <br />
            12. The lab complemented my understanding of the lectures
            <br />
            <input type="radio" name="q12" value="5" />strongly agree
            <input type="radio" name="q12" value="4" />agree
            <input type="radio" name="q12" value="3" />neutral
            <input type="radio" name="q12" value="2" />disagree
            <input type="radio" name="q12" value="1" />strongly disagree
        </fieldset>
        <br />
        <br />

        <fieldset>
            <legend> Instructor Specific Questions</legend>
            1. The instructor clearly presented the skills to be learned
            <br />
            <input type="radio" name="q13" value="5" />strongly agree
            <input type="radio" name="q13" value="4" />agree
            <input type="radio" name="q13" value="3" />neutral
            <input type="radio" name="q13" value="2" />disagree
            <input type="radio" name="q13" value="1" />strongly disagree
            <br />
            <br />
            2. The instructor effectively presented concepts and techniques
            <br />
            <input type="radio" name="q14" value="5" />strongly agree
            <input type="radio" name="q14" value="4" />agree
            <input type="radio" name="q14" value="3" />neutral
            <input type="radio" name="q14" value="2" />disagree
            <input type="radio" name="q14" value="1" />strongly disagree
            <br />
            <br />
            3. The instructor presented content in an organized manner
            <br />
            <input type="radio" name="q15" value="5" />strongly agree
            <input type="radio" name="q15" value="4" />agree
            <input type="radio" name="q15" value="3" />neutral
            <input type="radio" name="q15" value="2" />disagree
            <input type="radio" name="q15" value="1" />strongly disagree
            <br />
            <br />
            4. The instructor effectively presented the tools (e.g. materials, skills, and techniques) needed
            <br />
            <input type="radio" name="q16" value="5" />strongly agree
            <input type="radio" name="q16" value="4" />agree
            <input type="radio" name="q16" value="3" />neutral
            <input type="radio" name="q16" value="2" />disagree
            <input type="radio" name="q16" value="1" />strongly disagree
            <br />
            <br />
            5. The instructor explained concepts clearly
            <br />
            <input type="radio" name="q17" value="5" />strongly agree
            <input type="radio" name="q17" value="4" />agree
            <input type="radio" name="q17" value="3" />neutral
            <input type="radio" name="q17" value="2" />disagree
            <input type="radio" name="q17" value="1" />strongly disagree
            <br />
            <br />
            6. The instructor made the elements of good writing clear
            <br />
            <input type="radio" name="q18" value="5" />strongly agree
            <input type="radio" name="q18" value="4" />agree
            <input type="radio" name="q18" value="3" />neutral
            <input type="radio" name="q18" value="2" />disagree
            <input type="radio" name="q18" value="1" />strongly disagree
            <br />
            <br />
            7. The instructor clearly articulated the standards of performance for the course
            <br />
            <input type="radio" name="q19" value="5" />strongly agree
            <input type="radio" name="q19" value="4" />agree
            <input type="radio" name="q19" value="3" />neutral
            <input type="radio" name="q19" value="2" />disagree
            <input type="radio" name="q19" value="1" />strongly disagree
            <br />
            <br />
            8. The instructor provided guidance for understanding course exercises
            <br />
            <input type="radio" name="q20" value="5" />strongly agree
            <input type="radio" name="q20" value="4" />agree
            <input type="radio" name="q20" value="3" />neutral
            <input type="radio" name="q20" value="2" />disagree
            <input type="radio" name="q20" value="1" />strongly disagree
        </fieldset>

I want to be able to display each result for each question in the same multi-line textbox like so but it be split between courses and professor questions: 我希望能够像这样在同一行多行文本框中显示每个问题的每个结果,但将其分为课程和教授问题:

COURSES 课程

Q1: Disagree 问题1:不同意

Q2: Neutral Q2:中立

Q3: Neutral Q3:中立

etc... 等等...

PROFESSOR 教授

Q1: Disagree 问题1:不同意

Q2: Neutral Q2:中性

Q3: Neutral Q3:中立

etc... 等等...

First: It's because you've not incremented the q value, 第一:这是因为您没有增加q值,

Incorrect because q is always 1: 不正确,因为q始终为1:

txtResults.Text = "Course Results";
if (results[i] == 1)
{
    txtResults.Text += "Q" + q + ": Strongly Disagree";
}

Second, you've allowed the program to write for cases where i=0 and i=11 其次,您已允许该程序针对i = 0和i = 11的情况进行编写

Full answer: 完整答案:

public void DisplayResults(double[] results)
{
    try
    {  
        for (int i = 0, q = 1; i <= 19; i++, q++)
        {
            if (i == 0)
            {
                txtResults.Text += "Course Results";
            }
            if (i == 11)
            {
                txtResults.Text += "Professor Results";
            }
            txtResults.Text += "Q" + q ": ";
            if (results[i] == 1)
            {
                txtResults.Text +="Strongly Disagree";
            }
            else if (results[i] == 2)
            {
                txtResults.Text +="Disagree";
            }
            else if (results[i] == 3)
            {
                txtResults.Text +=": Neutral";
            }
            else if (results[i] == 4)
            {
                txtResults.Text +="Agree";
            }
            else if (results[i] == 5)
            {
                if(i == 0 )
                txtResults.Text +="Strongly Agree ZERO";
                else if(i == 11 )
                txtResults.Text +="Strongly Agree ELEVEN";
                else
                txtResults.Text +="Strongly Agree";
            }
        }
    }
    catch (Exception e)
    {
        Console.Write(e.Message, "Error");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用for循环从文本框中的单选按钮值计算和显示平均成绩? - How to calculate and display average grade from radio button values in a textbox using a for loop? 将值从文本框/单选按钮传递到一个文本框 - Pass Values from Textboxes / Radio buttons to One TextBox 如何根据所选的单选按钮显示和验证文本框 - How to display and validate textbox based on radio button selected C#如何使Menustrip中的一项成为默认选择? 如何显示从“选定的菜单”到文本框中的选定项目 - C# How to make one item from Menustrip be default selected? How to display the selected items from Menustrip Selected to a textbox 如何在文本框中显示所选记录 - How to display a selected record in a textbox 一个组件中的 Blazor 单选按钮从其他组件中取消选择单选按钮 - Blazor radio buttons in one component unselecting radio buttons from the others 如何基于下拉列表中的选定值在文本框中显示数据 - How to display data on textbox based on selected value from dropdownlist 如何从数据库加载选定的数据并以另一种形式显示在文本框中 - how to load selected data from database and display in textbox in another form 选定的单选按钮错误 - Selected Radio Buttons Error 如何在列表框中显示所选数据并在文本框中显示? - How to display selected data in Listbox and display in Textbox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM