在C#中控制动态复选框

时间:2014-09-25 06:27:36

标签: c# asp.net checkbox

我的任务是从数据库中读取条目并添加与每个条目对应的复选框
我已打开数据库连接并创建了复选框,但无法检查复选框的设置。我正在使用静态数组ck []

存储我的复选框
int count = 0;
while(dr.Read())  //Reading the Database
{
   CheckBox temp = new CheckBox();  //Creating a temprory checkbox
   temp.Text = dr[1].ToString();    
   temp.Checked = false;  //Making the checkbox intitally empty
   temp.ID = dr[0].ToString();  
   ck[count] = temp;  //inserting the checkbox's references into the array
   ck[count].CheckedChanged += new EventHandler(temp_CheckedChanged);  //Adding the even handler
   Panel1.Controls.Add(ck[count]); //Adding the checkbox to panel
   LiteralControl lc = new LiteralControl("<br>");
   Panel1.Controls.Add(lc);
   count++;
}
void temp_CheckedChanged(object sender, EventArgs e) //handling the checking and unchecking of the checkbox
{
   CheckBox ckb = (CheckBox)sender;
   ck[index].Checked = !ck[index].Checked;
}

问题出现在复选框的 .Checked 条目中没有更新。
我在这里阅读了很多帖子并试图实现它们,但到目前为止还没有任何结果。我甚至尝试从我正在显示它的窗格中读取数据。

1 个答案:

答案 0 :(得分:0)

最好使用checkboxlist。因为您可以在运行时添加项目并像列表一样工作。