Depending on the value of a field 'type' in a database, I want to generate either a CheckBoxList (if 'type' is C) or a RadioButtonList (if 'type' is R), and then populate them accordingly. I've managed to create the necessary control, however, in the populateAnswers method, I'm not sure how to access the newly created control. You can see from the code below that I've tried
((CheckBoxList)this.FindControl("cblAnswers"))
but that doesn't work. Any help would be greatly appreciated.
Code:
protected void Button1_Click(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
MySqlConnection conn = new MySqlConnection(connStr);
MySqlDataReader reader;
List<string> listOfAnswerIDs = new List<string>();
List<string> listOfAnswers = new List<string>();
List<string> listOfCorrectAnswerIDs = new List<string>();
try
{
conn.Open();
string cmdText = "SELECT * FROM questions_m WHERE question_id=1";
MySqlCommand cmd = new MySqlCommand(cmdText, conn);
reader = cmd.ExecuteReader();
if (reader.Read())
{
lblQuestion.Text = reader["question"].ToString();
if (reader["type"].ToString().Equals("C"))
{
CheckBoxList cblAnswers = new CheckBoxList();
Page.Form.Controls.Add(cblAnswers);
}
else if (reader["type"].ToString().Equals("R"))
{
RadioButtonList rblAnswers = new RadioButtonList();
Page.Form.Controls.Add(rblAnswers);
}
ViewState["QuestionID"] = reader["question_id"].ToString();
reader.Close();
string cmdText2 = "SELECT * FROM answers_m WHERE question_id=1";
MySqlCommand cmdAnswers = new MySqlCommand(cmdText2, conn);
reader = cmdAnswers.ExecuteReader();
while (reader.Read())
{
listOfAnswerIDs.Add(reader["answer_id"].ToString());
listOfAnswers.Add(reader["answer"].ToString());
if (reader["correct"].ToString().Equals("Y"))
{
listOfCorrectAnswerIDs.Add(reader["answer_id"].ToString());
}
}
reader.Close();
populateAnswers(listOfAnswers, listOfAnswerIDs);
}
else
{
reader.Close();
lblError.Text = "(no questions found)";
}
ViewState["listOfCorrectAnswerIDs"] = listOfCorrectAnswerIDs;
}
catch
{
lblError.Text = "Database connection error - failed to read records.";
}
finally
{
conn.Close();
}
}
private void populateAnswers(List<string> listOfAnswers, List<string> listOfAnswerIDs)
{
Random ran = new Random(Guid.NewGuid().GetHashCode());
var numbers = Enumerable.Range(1, listOfAnswers.Count).OrderBy(j => ran.Next()).ToList();
List<ListItem> ans = new List<ListItem>();
for (int i = 0; i < listOfAnswers.Count; i++)
{
ans.Add(new ListItem(listOfAnswers[i].ToString(), listOfAnswerIDs[i].ToString()));
}
foreach (int num in numbers)
{
((CheckBoxList)this.FindControl("cblAnswers")).Items.Add(ans[num - 1]);
}
}
When you create the CheckBoxList or the RadioButtonList, try giving them an ID. When you use FindControl(), it looks for a control with the specific ID you pass it.
CheckBoxList cblAnswers = new CheckBoxList();
cblAnswers.ID = "cblAnswers";
Page.Form.Controls.Add(cblAnswers);
RadioButtonList rblAnswers = new RadioButtonList();
rblAnswers.ID = "rblAnswers";
Page.Form.Controls.Add(rblAnswers);