从代码隐藏文件动态地将数据加载到Repeater中

时间:2015-06-25 11:05:09

标签: c# asp.net

问题可能有点长:

我正在建立一个在线评估门户网站,并且数据库中存在不同类型的问题需要在网络表单中加载。问题可以是单一答案(单选按钮)或多方答案(复选框按钮)。因此,根据类型,我根据需要创建单选按钮或复选框,并在运行时将其加载到Repeater中。代码如下:

SqlCommand cmd = new SqlCommand("select * from tbl_QuestionAnswer", con);
SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())
{
    if ((string)dr["type"] == "SingleAnswer")
    {
        RadioButton rAnswer1 = new RadioButton();
        rAnswer1.ID = "rbl_Answer1";
        rAnswer1.GroupName = "QAnswer1";
        rAnswer1.Text = dr["Answer1"].ToString();
        Panel PAnswer1 = e.Item.FindControl("PAnswer1") as Panel;
        PAnswer1.Controls.Add(rAnswer1);


        RadioButton rAnswer2 = new RadioButton();
        rAnswer2.ID = "rbl_Answer2";
        rAnswer2.GroupName = "QAnswer1";
        rAnswer2.Text = dr["Answer2"].ToString();
        Panel PAnswer2 = e.Item.FindControl("PAnswer2") as Panel;
        PAnswer2.Controls.Add(rAnswer2);


        RadioButton rAnswer3 = new RadioButton();
        rAnswer3.ID = "rbl_Answer3";
        rAnswer3.GroupName = "QAnswer1";
        rAnswer3.Text = dr["Answer3"].ToString();
        Panel PAnswer3 = e.Item.FindControl("PAnswer3") as Panel;
        PAnswer3.Controls.Add(rAnswer3);


        RadioButton rAnswer4 = new RadioButton();
        rAnswer4.ID = "rbl_Answer4";
        rAnswer4.GroupName = "QAnswer1";
        rAnswer4.Text = dr["Answer4"].ToString();
        Panel PAnswer4 = e.Item.FindControl("PAnswer4") as Panel;
        PAnswer4.Controls.Add(rAnswer4);
    }

    else
    {
        CheckBox cAnswer1 = new CheckBox();
        cAnswer1.ID = "chk_Answer1";
        cAnswer1.Text = dr["Answer1"].ToString();
        Panel PAnswer1 = e.Item.FindControl("PAnswer1") as Panel;
        PAnswer1.Controls.Add(cAnswer1);

        CheckBox cAnswer2 = new CheckBox();
        cAnswer2.ID = "chk_Answer2";
        cAnswer2.Text = dr["Answer2"].ToString();
        Panel PAnswer2 = e.Item.FindControl("PAnswer2") as Panel;
        PAnswer2.Controls.Add(cAnswer2);

        CheckBox cAnswer3 = new CheckBox();
        cAnswer3.ID = "chk_Answer3";
        cAnswer3.Text = dr["Answer3"].ToString();
        Panel PAnswer3 = e.Item.FindControl("PAnswer3") as Panel;
        PAnswer3.Controls.Add(cAnswer3);

        CheckBox cAnswer4 = new CheckBox();
        cAnswer4.ID = "chk_Answer4";
        cAnswer4.Text = dr["Answer4"].ToString();
        Panel PAnswer4 = e.Item.FindControl("PAnswer4") as Panel;
        PAnswer4.Controls.Add(cAnswer4);
    }
}

但问题是所有答案都会在所有问题中得到解决。即从该博士[“Answer1”]获取“Answer1”列中的所有数据,然后创建一个复选框或单选按钮。我不知道为什么会发生这种情况,因为它一次只能采用行和来自该行的数据。

这也很好,直到2个问题。之后,如果我添加第三个问题并尝试加载它,则会出现错误,说“找到了具有相同ID的多个控件'rbl_Answer1'。FindControl要求控件具有唯一ID。”这也是正确的。因此,我需要一种方法,根据单个答案或多个答案

动态地将问题加载到Web表单中

1 个答案:

答案 0 :(得分:1)

很明显,如果您的查询中有多行,则可以使用相同的ID 创建控件。

尝试制作动态字段/控件并为这些控件设置动态ID

while (dr.Read())
{
    if ((string)dr["type"] == "SingleAnswer")
    {
        RadioButton rAnswer1 = new RadioButton();
        rAnswer1.ID = "rbl_Answer" + dr["AnswerID1"].ToString();

或类似的......