I created a trivia page and need help with calculating the number of correct answers vs wrong answers. when customers click the summary button. How ever i also created a submit buttton for each question but i dont like that idea. I would like the users to answers the questions and then hit a submit button or summary and then it totals their score
Below is my code. Please advice
protected
void Q1btnSubmit_Click(object sender, EventArgs e){
if(RadioButtonList1.SelectedIndex >-1){
if (RadioButtonList1.SelectedIndex == 0)lblQ1Message.Text =
"You Selected: " + RadioButtonList1.SelectedItem ; elselblQ1Message.Text =
"Sorry that incorrect";}
elselblQ1Message.Text =
"Please make a selection";}
protected void Q2btnButton_Click(object sender, EventArgs e){
if(RadioButtonList2.SelectedIndex >-1){
if (RadioButtonList2.SelectedIndex == 2)
lblQ2Message.Text =
"You Selected: " + RadioButtonList2.SelectedItem ; elselblQ2Message.Text =
"Sorry that incorrect";}
elselblQ2Message.Text =
"Please make a selection";}
protected void Q3btnSubmit_Click(object sender, EventArgs e){
if (RadioButtonList3.SelectedIndex > -1){
if (RadioButtonList3.SelectedIndex == 4)
lblQ3Message.Text =
"You Selected: " + RadioButtonList3.SelectedItem; elselblQ3Message.Text =
"Sorry that incorrect";}
elselblQ3Message.Text =
"Please make a selection";}
protected void Q4btnSubmit_Click(object sender, EventArgs e){
if (RadioButtonList4.SelectedIndex > -1){
if (RadioButtonList4.SelectedIndex == 3)
lblQ4Message.Text =
"You Selected: " + RadioButtonList4.SelectedItem; elselblQ4Message.Text =
"Sorry that incorrect";}
elselblQ4Message.Text =
"Please make a selection";}
protected void Q5btnSubmit_Click(object sender, EventArgs e){
if (RadioButtonList5.SelectedIndex > -1){
if (RadioButtonList5.SelectedIndex == 0)
lblQ5Message.Text =
"You Selected: " + RadioButtonList5.SelectedItem; elselblQ5Message.Text =
"Sorry that incorrect";}
elselblQ5Message.Text =
"Please make a selection";}
protected void Q6btnSubmit_Click(object sender, EventArgs e){
if (RadioButtonList6.SelectedIndex > -1){
if (RadioButtonList6.SelectedIndex == 3)
lblQ6Message.Text =
"You Selected: " + RadioButtonList6.SelectedItem; elselblQ6Message.Text =
"Sorry that incorrect";}
elselblQ6Message.Text =
"Please make a selection";}
protected void Button1_Click(object sender, EventArgs e){
}
protected void btnSummary_Click(object sender, EventArgs e){
}
Niradhip ChakrabortyPosted May 3, 2009, 5:51 AM
Mike SandersPosted May 2, 2009, 10:02 PM
Still having issues. Can someone plese check my code to see if i'm doing somethign wrong
protected
void Q1btnSubmit_Click(object sender, EventArgs e){
if (RadioButtonList1.SelectedIndex > -1){
if (RadioButtonList1.SelectedIndex == 0)lblQ1Message.Text =
"You Selected: " + RadioButtonList1.SelectedItem; elselblQ1Message.Text =
"Sorry that incorrect";}
elselblQ1Message.Text =
"Please make a selection";}
protected void Q2btnButton_Click(object sender, EventArgs e){
if (RadioButtonList2.SelectedIndex > -1){
if (RadioButtonList2.SelectedIndex == 2)lblQ2Message.Text =
"You Selected: " + RadioButtonList2.SelectedItem; elselblQ2Message.Text =
"Sorry that incorrect";}
elselblQ2Message.Text =
"Please make a selection";}
protected void Q3btnSubmit_Click(object sender, EventArgs e){
if (RadioButtonList3.SelectedIndex > -1){
if (RadioButtonList3.SelectedIndex == 4)lblQ3Message.Text =
"You Selected: " + RadioButtonList3.SelectedItem; elselblQ3Message.Text =
"Sorry that incorrect";}
elselblQ3Message.Text =
"Please make a selection";}
protected void Q4btnSubmit_Click(object sender, EventArgs e){
if (RadioButtonList4.SelectedIndex > -1){
if (RadioButtonList4.SelectedIndex == 3)lblQ4Message.Text =
"You Selected: " + RadioButtonList4.SelectedItem; elselblQ4Message.Text =
"Sorry that incorrect";}
elselblQ4Message.Text =
"Please make a selection";}
protected void Q5btnSubmit_Click(object sender, EventArgs e){
if (RadioButtonList5.SelectedIndex > -1){
if (RadioButtonList5.SelectedIndex == 0)lblQ5Message.Text =
"You Selected: " + RadioButtonList5.SelectedItem; elselblQ5Message.Text =
"Sorry that incorrect";}
elselblQ5Message.Text =
"Please make a selection";}
protected void Q6btnSubmit_Click(object sender, EventArgs e){
if (RadioButtonList6.SelectedIndex > -1){
if (RadioButtonList6.SelectedIndex == 3)lblQ6Message.Text =
"You Selected: " + RadioButtonList6.SelectedItem; elselblQ6Message.Text =
"Sorry that incorrect";}
elselblQ6Message.Text =
"Please make a selection";}
protected void Button1_Click(object sender, EventArgs e){
}
protected void btnSummary_Click(object sender, EventArgs e){
int intPoints, intTotal = 0; for (intPoints = 0; intPoints < 1; intPoints++){
//for (intPoints = 0; intPoints <1; intPoints --)
if (RadioButtonList1.SelectedIndex == 0){
intPoints = 1;
//intPoints ++; if(RadioButtonList1.SelectedIndex != 0)intPoints--;
if (RadioButtonList2.SelectedIndex == 2){
intPoints = intPoints + 1;
//intTotal ++; if (RadioButtonList2.SelectedIndex != 2)intPoints = intPoints - 1;
if (RadioButtonList3.SelectedIndex == 4){
intPoints = intPoints + 1;
//intTotal ++; if (RadioButtonList3.SelectedIndex != 4)intPoints = intPoints - 1;
if (RadioButtonList4.SelectedIndex == 3){
intPoints = intPoints + 1;
//intTotal ++; if (RadioButtonList4.SelectedIndex != 3)intPoints = intPoints - 1;
if (RadioButtonList5.SelectedIndex == 0){
intPoints = intPoints + 1;
//intTotal ++; if (RadioButtonList5.SelectedIndex != 0)intPoints = intPoints - 1;
if (RadioButtonList6.SelectedIndex == 3){
intPoints = intPoints + 1;
//intTotal ++; if (RadioButtonList5.SelectedIndex != 0)intPoints = intPoints - 1;
QSummaryLabel.Text =
"You Scored " + intTotal + " out of 6 Questions";}
}
}
}
}
}
}
}
}
Mike SandersPosted May 1, 2009, 1:27 PM
Niradhip ChakrabortyPosted May 1, 2009, 5:12 AM
You can have a set of radio button for each question. As soon as they click on submit button. You can calculate the score based on selected radio button.
Say question 1has two radio button “q1a” and “q1b”.
Same way question 2 has two radio button “q2a” and “q2b”
Same way it will continue for other questions.
Now you can validate it based one correct answer. Say for question 1, “q1b” holds the right answer and question 2 “q2a” hold it.
Now in submit button event you can have the following code. Say for each correct answer user will get 1 point.
protected void Button1_Click(object sender, EventArgs e)
{
int intPoint = 0;
//calculating the point for question 1;
if (q1b.Checked == true)
{
intPoint = 1;
}
//calculating the point for question 2;
if (q2a.Checked == true)
{
intPoint = intPoint + 1;
}
//and continues for other questions. At the end your “intPoint” variable having the total point.
}