2014-09-25 83 views
0

我的任務是從數據庫中讀取條目,把對應於每個條目
我已經打開了數據庫連接,並創建對應的複選框,但我無法檢查設置一個複選框的複選框。我使用的事實,那就是在複選框的.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

回答

0

更好地使用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