ASP.Net C# - 访问动态生成的List控件

时间:2014-07-15 18:18:42

标签: c# asp.net controls asp.net-controls

取决于字段的值' type'在数据库中,我想生成CheckBoxList(如果'类型为C)或RadioButtonList(如果'类型'是R),然后相应地填充它们。我设法创建了必要的控件,但是,在populateAnswers方法中,我不确定如何访问新创建的控件。您可以从下面的代码中看到我已尝试

((CheckBoxList)this.FindControl("cblAnswers"))

但这不起作用。任何帮助将不胜感激。

代码:

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]);
    }
}

1 个答案:

答案 0 :(得分:1)

创建CheckBoxList或RadioButtonList时,请尝试为其提供ID。当您使用FindControl()时,它会查找具有您传递的特定ID的控件。

CheckBoxList cblAnswers = new CheckBoxList();
cblAnswers.ID = "cblAnswers";
Page.Form.Controls.Add(cblAnswers);

RadioButtonList rblAnswers = new RadioButtonList();
rblAnswers.ID = "rblAnswers";
Page.Form.Controls.Add(rblAnswers);
相关问题