当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to get checkboxlist selected property as true

问题描述:

I my aspx page i am taking a checkboxlist control and populating data from database. But when i used to select the item and debug my code always selected property used to come as false.How to solve this.

My code is

 <asp:CheckBoxList ID="chkProduct" Width="200px" Height="90px" runat="server"

RepeatDirection="Vertical" style="overflow-y: scroll;

overflow-x: scroll;">

</asp:CheckBoxList>


Code Behind

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

}

}

}

网友答案:

You can lose the checked state if you are databinding the list on postback. You want to make sure that when you databind the list you only do it if Page.IsPostBack is false.

网友答案:

Were are you binding the check-box list.
It it is on the page load then it should check for is not postback as follow

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();
}
网友答案:

try this.. set autopostback property to TRUE of checkboxlist

分享给朋友:
您可能感兴趣的文章:
随机阅读: