在C#中控制动态复选框

问题描述:

我的任务是从数据库中读取条目,把对应于每个条目
我已经打开了数据库连接,并创建对应的复选框,但我无法检查设置一个复选框的复选框。我使用的事实,那就是在复选框的.Checked没有找到更新问题出现的静态数组CK []在C#中控制动态复选框

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

储存我复选框。
我在这里阅读了很多文章,并试图实现它们,但迄今为止没有任何成果。我甚至尝试读取我正在显示它的窗格中的数据。

+0

.Checked条目中没有更新?不清楚 – Lali 2014-09-25 06:38:41

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

+0

我看了一些帖子,并知道它已被弃用。因此我没有使用它。尽管如此,请告诉我它是否好用? – 2014-09-25 06:36:53

+0

在你的代码中,我发现只有一个问题。您应该在'!Page.IsPostback'中创建它,并在'PreInit事件'上重新创建这些控件。并且你已经阅读它已经被弃用。 – 2014-09-25 06:39:04

+0

我正在尝试更改按钮单击时的值。我不认为这需要.IsPostBack。否则请通知我 – 2014-09-25 06:41:29