我根据Button_Click事件中的条件(可行)动态创建RadioButtonList或CheckBoxList。
protected void btnGetQuestion_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>();
int questionCounter = 0;
//get questionIDs and store in ViewState["listOfQuestionIDs"]
getListOfQuestionIDs();
try
{
conn.Open();
string cmdText = "SELECT * FROM questions_m WHERE question_id=@QuestionID";
MySqlCommand cmd = new MySqlCommand(cmdText, conn);
cmd.Parameters.Add("@QuestionID", MySqlDbType.Int32);
cmd.Parameters["@QuestionID"].Value = listOfQuestionIDs[questionCounter];
reader = cmd.ExecuteReader();
if (reader.Read())
{
lblQuestion.Text = reader["question"].ToString();
if (reader["type"].ToString().Equals("C"))
{
CheckBoxList cblAnswers = new CheckBoxList();
cblAnswers.ID = "cblAnswers";
Page.Form.Controls.Add(cblAnswers);
}
else if (reader["type"].ToString().Equals("R"))
{
RadioButtonList rblAnswers = new RadioButtonList();
rblAnswers.ID = "rblAnswers";
Page.Form.Controls.Add(rblAnswers);
}
questionCounter += 1;
ViewState["questionCounter"] = questionCounter;
ViewState["QuestionID"] = Convert.ToInt32(reader["question_id"]);
reader.Close();
string cmdText2 = "SELECT * FROM answers WHERE question_id=@QuestionID";
MySqlCommand cmdAnswers = new MySqlCommand(cmdText2, conn);
cmdAnswers.Parameters.Add("@QuestionID", MySqlDbType.Int32);
cmdAnswers.Parameters["@QuestionID"].Value = ViewState["QuestionID"];
reader = cmdAnswers.ExecuteReader();
while (reader.Read())
{
listOfAnswerIDs.Add(reader["answer_id"].ToString());
listOfAnswers.Add(reader["answer"].ToString());
}
reader.Close();
populateAnswers(listOfAnswers, listOfAnswerIDs);
}
reader.Close();
}
catch
{
lblError.Text = "Database connection error - failed to read records.";
}
finally
{
conn.Close();
}
}
我想创建一个方法,我可以在另一个按钮点击事件(btnNext_Click)中运行,该事件将删除RadioButtonList或CheckBoxList(如果存在)。
我已尝试过以下操作,但它似乎无法正常工作:
protected void clearAnswers()
{
if (((CheckBoxList)this.FindControl("cblAnswers")) != null)
{
Page.Form.Controls.Remove(this.FindControl("cblAnswers"));
}
if (((RadioButtonList)this.FindControl("rblAnswers")) != null)
{
Page.Form.Controls.Remove(this.FindControl("rblAnswers"));
}
}
更新: 我认为我在RadioButtonList / CheckBoxList的重新填充中遇到了这个问题。如果我在重新填充它们之前清除了每个项目,那就解决了我的问题。
if (((CheckBoxList)this.FindControl("cblAnswers")) != null)
{
((CheckBoxList)this.FindControl("cblAnswers")).Items.Clear();
foreach (int num in numbers)
{
((CheckBoxList)this.FindControl("cblAnswers")).Items.Add(ans[num - 1]);
}
}
if (((RadioButtonList)this.FindControl("rblAnswers")) != null)
{
((RadioButtonList)this.FindControl("rblAnswers")).Items.Clear();
foreach (int num in numbers)
{
((RadioButtonList)this.FindControl("rblAnswers")).Items.Add(ans[num - 1]);
}
}
答案 0 :(得分:0)
试试这个:
protected void clearAnswers()
{
CheckBoxList cblAnswers = (CheckBoxList)this.FindControl("cblAnswers");
RadioButtonList rblAnswers = (RadioButtonList)this.FindControl("rblAnswers");
if (cblAnswers != null)
{
Page.Form.Controls.Remove(cblAnswers);
}
if (rblAnswers != null)
{
Page.Form.Controls.Remove(rblAnswers);
}
}