In an asp.net web forms application I am pulling in a random set of up to 2 questions and up to 3 answers (for each question) from an SQL database. For each question I am dynamically creating a RadioButtonList control to hold the set of answers.
When a postback is triggered I need to read the value of each RadioButtonList. I don't need to retain the control itself just the selected index/value because a new (different) set of questions and answers will be created if an answer is wrong and the user gets redirected to a new page if the answers are correct.
I have tried to re-create the controls in the Page_init event during the postback but because my dynamic controls are created based on a random set of data, they will be different each time.
Dynamic control creation on Page_Load:
foreach (DataRow question in dt_questions.Rows)
{
questionCounter++;
RadioButtonList RBL_Answers = new RadioButtonList();
RBL_Answers.ID = "RBL_Answers_" + questionCounter;
// fill radio list with answers to pick from
foreach (DataRow answer in dt_answers.Rows)
{
ListItem item = new ListItem(answer["IA_Text"].ToString(), answer["IA_Correct"].ToString());
RBL_Answers.Items.Add(item);
}
Panel_ContentQA.Controls.Add(RBL_Answers);
}