Solved

a group of radiobuttonlist items

Posted on 2011-02-16
13
737 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

810 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