C# 如何将checkboxlist selected属性设置为true
我在我的aspx页面中使用了一个checkboxlist控件,并从数据库中填充数据。但是,当我用来选择项目并调试代码时,我的“始终选择”属性通常为false。如何解决此问题。C# 如何将checkboxlist selected属性设置为true,c#,asp.net,C#,Asp.net,我在我的aspx页面中使用了一个checkboxlist控件,并从数据库中填充数据。但是,当我用来选择项目并调试代码时,我的“始终选择”属性通常为false。如何解决此问题。我的代码是 <asp:CheckBoxList ID="chkProduct" Width="200px" Height="90px" runat="server" RepeatDirection="Vertical" style="overflow-y: scroll;
我的代码是
<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);
}
}
}
for(int j=0;j
如果在回发时对列表进行数据绑定,则可能会丢失选中状态。您希望确保在对列表进行数据绑定时,仅当您正在绑定复选框列表时,Page.IsPostBack
为false
才执行此操作。
如果它在页面加载中,那么它应该检查是否未按如下方式回发
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();
}
if(!Page.IsPostBack)
{
//把你的绑定代码放在这里
List productList=commonClient.GetProductList(string.Empty,null,1);
chkProduct.DataSource=productList.OrderBy(i=>i.Name);
chkProduct.DataTextField=“ProductCodeWithName”;
chkProduct.DataValueField=“ID”;
chkProduct.DataBind();
}
试试这个。。
将checkboxlist的autopostback属性设置为TRUE请将绑定代码也设置为TRUE。在页面加载上可能有您正在绑定的区域。请检查在绑定checkboxlistList productList=commonClient.GetProductList(string.Empty,null,1)时您的页面回发事件;chkProduct.DataSource=productList.OrderBy(i=>i.Name);chkProduct.DataTextField=“ProductCodeWithName”;chkProduct.DataValueField=“ID”;chkProduct.DataBind();您在哪里执行该代码?在页面中加载?尝试将完整的页面加载(或任何进行数据绑定的方法)添加到原始问题中。但是您还需要确保代码包装在
if(!Page.IsPostBack){//your code}
中。如果在回发时进行数据绑定,则会丢失对列表所做的任何更改。就像你在重新创建列表一样。我认为你需要在问题中发布更多的代码。请尝试将full Page_Load方法和checkboxlist事件处理程序方法放在其中(或submit按钮,无论触发回发事件的是什么)。谢谢。如果“自动回发”为真,会发生什么?回发后复选框列表中的更改是否仍然存在?