Click here to Skip to main content
15,889,838 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting values from radio buttons with id these radio button are in repeater . Correct option in rd4. I am comparing rd1,rd2, & rd3 with rd4 if any one matched then count+1 else count remains 0 then this count result i want to show in label .but result continuously shows 0 wether it is correct or not...

What I have tried:

 protected void Button1_Click(object sender, EventArgs e)
        {
            int count1=0 ;

           
            if (Repeater1.Items.Count > 0)
            {
                for (int count = 0; count < Repeater1.Items.Count; count++)
                {
                    RadioButton rd1 = (RadioButton)Repeater1.Items[count].FindControl("RadioButton1");
                    RadioButton rd2 = (RadioButton)Repeater1.Items[count].FindControl("RadioButton2");
                    RadioButton rd3 = (RadioButton)Repeater1.Items[count].FindControl("RadioButton3");
                    RadioButton rd4 = (RadioButton)Repeater1.Items[count].FindControl("RadioButton31");
    if (rd1.Checked)
                    {
                        if (rd1 == rd4)
                        {
                            count1++;
                        }
                        else
                        {
                            count1 = count1 + 0;
                        }


                    }
                    if (rd2.Checked)
                    {
                        if (rd2 == rd4)
                        {
                            count1++;
                        }
                        else
                        {
                            count1 = count1 + 0;
                        }

                    }
                    if (rd3.Checked)
                    {
                        if (rd3 == rd4)
                        {
                            count1++;
                        }
                        else
                        {
                            count1 = count1 + 0;
                        }

                    }

            Label1.Text = Convert.ToString(count1);
}
Posted
Updated 20-Oct-17 0:15am
Comments
Richard Deeming 18-Oct-17 14:48pm    
You're testing for reference equality. Two different instances of the RadioButton control will never be the same reference, so your tests will never succeed.

It's not clear why you're using another RadioButton for the correct answer. Particularly since the user will be able to inspect the control to see what the correct answer is.

Also, all of your else blocks which add zero to the variable are redundant. Adding zero to a number will not make any difference.

1 solution

Why not simply assign a value, e.g "1" to the correct answer (it's radio button), then put that condition that adds the count for score if the value is 1.

e.g
if(rd=="1")
{
count += 1;
return
}
Label1.Text = Convert.ToString(count1);
 
Share this answer
 
Comments
Richard Deeming 20-Oct-17 11:04am    
You can't apply the equality operator to a RadioButton and a string.

The return statement exits the current function without updating the label.

And the user would be able to view the page source to see which option was the correct answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900