Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

a group of radiobuttonlist items

Posted on 2011-02-16
13
Medium Priority
?
741 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 9

Accepted Solution

by:
rawinnlnx9 earned 1500 total points
ID: 34908826
 
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
ID: 34909006
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
ID: 34910989
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:mr_nadger
ID: 34915507
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
ID: 34917224
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
ID: 34917237
is each question a seperate radiobuttonlist, with one item per possible answer?

0
 
LVL 6

Author Comment

by:r3nder
ID: 34917273
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
ID: 34917439
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
ID: 34917578
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
ID: 34929466

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

Open in new window

0
 
LVL 6

Author Comment

by:r3nder
ID: 34943516
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
ID: 34943531
It wasnt absolutely correct - but it got me on the right track
0

Featured Post

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

610 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