Link to home
Start Free TrialLog in
Avatar of r3nder
r3nderFlag for United States of America

asked on

a group of radiobuttonlist items

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 :)
ASKER CERTIFIED SOLUTION
Avatar of rawinnlnx9
rawinnlnx9
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of r3nder

ASKER

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>
Avatar of r3nder

ASKER

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

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.

Avatar of r3nder

ASKER

I guess my question should be "How do I enumerate through how many questions were answered wrong"
is each question a seperate radiobuttonlist, with one item per possible answer?

Avatar of r3nder

ASKER

yes - in the example above if msg +="Incorrect" (which only appears when it is wrong)
then add 1 to the count
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
I guess the response to your asking how to count the wrongly answered questions, is how are you checking the selected answer is correct?
Avatar of Obadiah Christopher

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

Open in new window

Avatar of r3nder

ASKER

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());
Avatar of r3nder

ASKER

It wasnt absolutely correct - but it got me on the right track