Solved

a group of radiobuttonlist items

Posted on 2011-02-16
13
738 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Survey branching tutorial 11 44
ASP.NET Built-In Report Creator / Viewer 5 25
Broken .resx file generating errors 18 25
aspx ascx, c# 7 19
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
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.

726 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