【问题标题】:ASP.Net C# - Remove controls dynamicallyASP.Net C# - 动态删除控件
【发布时间】:2014-09-06 22:37:46
【问题描述】:

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

【问题讨论】:

  • 当你运行它时它会抛出任何类型的错误或异常吗?
  • @bhav 到底什么不起作用?请进一步解释。如果您进行完整的回发,那么您的动态控件无论如何都会消失...
  • @PseudoNym01 不,下面这行代码Page.Form.Controls.Remove(this.FindControl("cblAnswers"));似乎没有删除 CheckBoxList 并且我没有收到任何错误或异常。
  • 与 FindControl 一起使用时,什么上下文有“this”?
  • 你确定这条线正在被执行吗?

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


【解决方案1】:

试试这个:

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

【讨论】:

  • 无法将类型“System.Web.UI.Control”隐式转换为“System.Web.UI.WebControls.CheckBoxList”
猜你喜欢
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多