[RESOLVED] Check one radiobutton from a set of them!
I am creating a table containing the grades pupils received for certain skills. I have created a set of buttons, each representing a different skill, and created a set of radiobuttons alongside each skill button. For each skill one and only one radiobutton is checked to show the grade the pupil received. I.e. if the pupil received a "1" then the first radiobutton would be selected but if the pupil received a "5" then the fifth radiobutton would be selected. Here's the essentials of my code:
Code:
// Create a set of buttons
for (int i = 0; i < skillList.Length; i++)
{
btnSkill = new Button();
btnSkill.ID = skillList[i].ID; // This comes from the webservice
// For every btnSkill add a number of radiobuttons
for (int x = 0; x < gradeList.Length; x++)
{
int grade = x + 1;
rbtnSkill = new RadioButton();
rbtnSkill.ID = skillList[i].Name + "_" + grade;
rbtnSkill.Checked = false;
rbtnSkill.AutoPostBack = true;
rbtnSkill.CheckedChanged += new EventHandler(rbtnSkill_CheckedChanged);
}
}
On loading the page I would like the corresponding radiobuttons to be checked (this information should come from the webservice). I am however only able to check one radiobutton instead of all of them since rbtnSkill only refers to the last created/selected radiobutton! How do I overcome this problem?
Code:
// Only checks ONE rbtn
protected void GetEvaluationData()
{
// For each rbtn in the list, i.e. all 10
for (int i = 0; i < skillList.Length; i++)
{
// Split rbtn ID to return a string with the rbtn's skill name and another string containing the rbtns grade
SplitRbtnID(rbtnSkill.ID);
// Loop through each group of rbtns
// If rbtnID in question has this groupname
if (rbtnIDList[0] == skillList[i].Name)
{
// Loop through whole loop to see if equals current position in grade group
for(int a = 0; a < gradeList.Length; a++)
{
// If equals current eNumGrade
if(rbtnIDList[1] == gradesList[a])
{
// Check rbtn
rbtnSkill.Checked = true;
}
}
}
}
}
Re: Check one radiobutton from a set of them!
Hi there.
Is rbtnSkill a class wide variable? It's not defined within the context of the method you are using it in. Does it represent a concrete radio button or did you use to render all of your radio buttons? If you did you might as well ignore it is meaningless to you. Do you have the radio buttons in a grid view or something similar? If so you can iterate through the rows and columns to get the correct radio button. For example the 5th radio button would be on 5th column or something like that. So you could use that kind of approach to check the radio button for grade "5". You could extend this reasoning for all the other grades as well.