How to get values from Repeater control
I am using repeater control to display online question paper to user. I am showing 50 questions to user. And i am giving 4 check boxes for every question to select answer. Now my doubt is how to get all 50 options that checked by user, and to compare those answers with correct answer tag in my XML. I am using XML file, not database.
Can anyone please help me how to achi开发者_如何学Goeve this functionality?
You have to iterate Repeater
control, like...
if (Repeater1.Items.Count > 0)
{
for (int count = 0; count < Repeater1.Items.Count; count++)
{
CheckBox chk = (CheckBox)Repeater1.Items[count].FindControl("CheckBox1");
if (chk.Checked)
{
}
}
}
To get access to Repeater
items you should use:
repeaterId.Items
To get access to all checked controls of a repeater (which are definitely RadioButton
controls, as you should have one option per question), you can use:
foreach (ListViewDataItem item in repeaterId.Items)
{
// Finding RadioButton controls by Id
RadioButton firstOption = ((RadioButton)item.FindControl("firstOption"));
RadioButton secondOption = ((RadioButton)item.FindControl("secondOption"));
RadioButton thirdOption = ((RadioButton)item.FindControl("thirdOption"));
RadioButton fourthOption = ((RadioButton)item.FindControl("fourthOption"));
// Here you have four RadioButtones and you should only see which one of them is clicked. Then compare its value to correct value in your XML file.
}
精彩评论