2012-12-13 57 views
3

我的aspx頁面我正在使用checkboxlist控件並從數據庫填充數據。但是,當我用來選擇項目和調試我的代碼總是選定的屬性用來作爲false.How解決這個問題。

我的代碼是如何獲得checkboxlist選定的屬性爲真

<asp:CheckBoxList ID="chkProduct" Width="200px" Height="90px" runat="server"  
RepeatDirection="Vertical" style="overflow-y: scroll; 
          overflow-x: scroll;"> 
         </asp:CheckBoxList> 


代碼隱藏

for (int j = 0; j < chkProduct.Items.Count; j++) 
    { 
     //CheckBoxList chkProduct; 
     foreach (ListItem list in chkProduct.Items) 
     { 
      if (list.Selected) 
      { 
       enquiryProducts = new Services.EnquiryProduct(); 
       enquiryProducts.IsDelete = false; 
       enquiryProducts.ProductID = Convert.ToInt32(chkProduct.Items[j].Value); 
       enquiryProductList.Add(enquiryProducts); 
      } 
     } 
    } 
+2

請把綁定代碼ASLO 。您可能會在頁面加載中綁定一個區域。 –

+0

請檢查您的頁面posback事件,當您綁定checkboxlist – user1102001

回答

1

如果你是數據綁定在回傳列表中你可以失去的選中狀態。你想確保當你綁定列表時,只有在Page.IsPostBackfalse時纔會這樣做。

+0

列表 productList = commonClient.GetProductList(string.Empty,null,1); chkProduct.DataSource = productList.OrderBy(i => i.Name); chkProduct.DataTextField =「ProductCodeWithName」; chkProduct.DataValueField =「ID」; chkProduct.DataBind(); – user1840936

+0

你在哪裏執行該代碼?在Page_Load中?嘗試將完整的Page_Load(或其他數據綁定方法)添加到原始問題中。但是你也想確保代碼被封裝在一個'if(!Page.IsPostBack){//你的代碼}'中。如果您在回發數據綁定,您將失去對列表進行的任何更改。這就像你重新創建列表一樣。 – rsbarro

+0

我把它包裹起來,但在jQuery的移動它不起作用 – user1840936

0

你是否綁定了複選框列表。
它是在頁面加載,那麼它應該檢查是不是回傳如下

if (!Page.IsPostBack) 
{ 
    //put your binding code here 
    List<CommonServices.Product> productList = commonClient.GetProductList(string.Empty, null, 1); 
    chkProduct.DataSource = productList.OrderBy(i => i.Name); 
    chkProduct.DataTextField = "ProductCodeWithName"; 
    chkProduct.DataValueField = "ID"; 
    chkProduct.DataBind(); 
} 
+0

我也檢查,但它仍然不工作] – user1840936

+0

做回覆後,複選框列表中的更改仍然存在? –

+0

是的,它回傳後 – user1840936

-1

試試這個.. 集AutoPostBack屬性到的CheckBoxList的TRUE

+0

如果autopostback是真的會發生什麼? –