2014-02-10 43 views
1

我一直在從web服務返回的信息中創建複選框列表。複選框按預期呈現,但是當我嘗試然後閱讀它們以檢查它們是否被選中時,代碼無法找到它們。從數組中動態創建複選框,然後遍歷它們以檢查選中

我創建了一個名爲planList的面板,並讓代碼循環創建了動態列表框 - 然後在按下按鈕時,它應該遍歷複選框列表以查看用戶是否選擇了任何值。除非動態創建,否則代碼似乎沒有選擇任何複選框。任何人都可以協助?目前,我只是試圖拉出來的ID,如果它拿起一個複選框

代碼:

planList.Controls.Add(new LiteralControl("<h2>Plan List </h2>")); 
foreach (string[] ar in ws.planS(this.txtGetDetails.Text)) { 
    CheckBox cb = new CheckBox(); 
    cb.Text = ar[1].ToString(); 
    cb.ID = ar[0];     
    planList.Controls.Add(cb);     
    planList.Controls.Add(new LiteralControl("<b> Application ID: " + ar[2] + "</b>"));     
    planList.Controls.Add(new LiteralControl("<br>")); 
} 

protected void Uploadbutton_Click1(object sender, System.EventArgs e) { 
    foreach (Control c in planList.Controls) { 
     CheckBox chx = c as CheckBox; 
     if (chx != null) { 
      var planid = c.ID; 
     } 
    } 
} 
+1

你什麼時候添加複選框? – Andrei

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

0

你必須重寫CreateChildControls方法和添加的複選框有。通過這個,您可以在加載ViewState之前創建控件樹,該ViewState包含檢查過的數據。

+0

謝謝 我在頁面加載後添加它們 - 已將它們添加到頁面加載中,現在可以讀取它們。 – Carl