Solved

a group of radiobuttonlist items

Posted on 2011-02-16
13
733 Views
Last Modified: 2012-05-11
I have a list of radiobutton lists RadioButtonList1 - 20 - I want to check each one to see what radio button was selected and count the correct items selected - if it is above a certain number correct they pass - if not they fail - also I want to use a label under each question to say correct or incorrect - any help would be appreciated :)
0
Comment
Question by:r3nder
13 Comments
 
LVL 9

Accepted Solution

by:
rawinnlnx9 earned 500 total points
Comment Utility
 
string msg = "" ;

   foreach(ListItem li in RadioButtonList.Items)
   {
      if(li.Selected == true)
         {
            msg += "<BR>" + li.Text + " is selected.";
         }
   }
   Label1.Text = msg;

Open in new window

0
 
LVL 6

Author Comment

by:r3nder
Comment Utility
I get your example - thank you
this accounts for what was selected - how do I write it to tell me if the item selected is correct - for example
Q: The sky is blue
A: True
A: False
I select true and that for this example would be correct so I count 1 as correct (up to 20) if I miss 5 or more I fail -If I miss 4 or less I pass

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
                <asp:ListItem>True</asp:ListItem>
                <asp:ListItem>False</asp:ListItem>
            </asp:RadioButtonList>
0
 
LVL 6

Author Comment

by:r3nder
Comment Utility
Here is my code - I am trying to count how many questions were wrong and total them any body know what I am doing wrong?
public void checkvalue(object sender, EventArgs e)
     {
         int count = 0;
         string msg = "";

         foreach (ListItem li in RadioButtonList1.Items)
         {
             
             if (li.Selected == true)
             {
                 if (li.Value == "False")
                     msg += "InCorrect";
                 count + 1;
             }
         }
         Label1.Visible = true;
         Label1.Text = msg;
         Response.Write(count.ToString());

Open in new window

0
 
LVL 7

Expert Comment

by:mr_nadger
Comment Utility
Rather than iterating through the whole list of items, you could use any one of the selected item properties to check.
e.g
RadioButtonList1.SelectedIndex
RadioButtonList1.SelectedItem
RadioButtonList1.SelectedValue

Looking at that example, do you have a radiobuttonlist with a seperate item each for Yes and No?
If so, how do you know which of the available selections are correct?
To me, it appears your code is going to each question (RadioButtonList), and counting the number of selected answers - which would give you a score equal to the number of RadioButtonList objects.

I'd build a data table, holding question numbers and correct answer values, then iterate through the RadioButtonLists comparing SelectedValue or SelectedText properties.
If the CorrectAnswer column in the table matches the SelectedValue/SelectedText value for the corresponding RadioButtonList, increase the score by one.

0
 
LVL 6

Author Comment

by:r3nder
Comment Utility
I guess my question should be "How do I enumerate through how many questions were answered wrong"
0
 
LVL 7

Expert Comment

by:mr_nadger
Comment Utility
is each question a seperate radiobuttonlist, with one item per possible answer?

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Author Comment

by:r3nder
Comment Utility
yes - in the example above if msg +="Incorrect" (which only appears when it is wrong)
then add 1 to the count
0
 
LVL 7

Expert Comment

by:mr_nadger
Comment Utility
you still need something somewhere to hold the correct answer, e.g. you have the question:

What is my favourite colour?
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
                <asp:ListItem>Red</asp:ListItem>
                <asp:ListItem>Blue</asp:ListItem>
                <asp:ListItem>Green</asp:ListItem>
                <asp:ListItem>Yellow</asp:ListItem>
                <asp:ListItem>Lilac</asp:ListItem>
</asp:RadioButtonList>


It's easy to pull the selected option, but how do you reference it?
Like I said, if you have a table built in the code behind to hold the answers, you would just need to check the selected value against the selected text

(pseudo code on the fly, please don't take it as syntactically correct, I don't have vis studio handy and can never remember C#)
Private answers As DataTable
answers.Columns.Add("Correct", Type.GetType("System.String"))
feedRow = answers.NewRow()
feedRow(0) = "Green"
answers.Rows.Add(feedRow)

then when the user presses the submit button

if answers(0)(0)=RadioButtonList1.SelectedItem.Text then score=score+1
0
 
LVL 7

Expert Comment

by:mr_nadger
Comment Utility
I guess the response to your asking how to count the wrongly answered questions, is how are you checking the selected answer is correct?
0
 
LVL 20

Expert Comment

by:informaniac
Comment Utility

if (li.Selected == true)
             {
                 if (li.Value == "False")
                     msg += "InCorrect";
                 count + 1;
                 break;
             }

Open in new window

0
 
LVL 6

Author Comment

by:r3nder
Comment Utility
public void checkvalue(object sender, EventArgs e)
     {
         int count = 0;
         string msg = "";

         foreach (ListItem li in RadioButtonList1.Items)
         {
             
             if (li.Selected == true)
             {
                 if (li.Value == "False")
                     msg += "InCorrect";
                 count = count + 1;
             }
         }
         Label1.Visible = true;
         Label1.Text = msg;
         Response.Write(count.ToString());
0
 
LVL 6

Author Closing Comment

by:r3nder
Comment Utility
It wasnt absolutely correct - but it got me on the right track
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now