gpt4 book ai didi

c# - 将 CheckBoxList 绑定(bind)到通用列表

转载 作者:太空宇宙 更新时间:2023-11-03 20:16:09 25 4
gpt4 key购买 nike

如何将 CheckBoxList 绑定(bind)到通用列表对象。此示例代码应该用于以下目的:

protected void Page_Load(object sender, EventArgs e)
{
// Works well with the datatable as a data source
//DataTable _entityList = new DataTable();
//_entityList.Columns.Add("Id", typeof(int));
//_entityList.Columns.Add("ProductName", typeof(string));
//_entityList.Rows.Add(new object[] { 1, "First" });
//_entityList.Rows.Add(new object[] { 2, "Second" });

// Doesn't work with the generic list as a data source
List<MyProduct> _entityList = new List<MyProduct>();
_entityList.Add(new MyProduct(1, "First"));
_entityList.Add(new MyProduct(2, "Second"));

cblProducts.DataSource = _entityList;
cblProducts.DataTextField = "ProductName";
cblProducts.DataValueField = "Id";
cblProducts.DataBind();
}

public class MyProduct
{
public int Id;
public string ProductName;
public bool selected;

public MyProduct(int id, string desc, bool slctd)
{
this.Id = id;
this.ProductName = desc;
this.selected = slctd;
}

public MyProduct()
{
// TODO: Complete member initialization
}
}

但是它抛出了一个运行时异常:

DataBinding:“Test.MyProduct”不包含名为“ProductName”的属性。

我错过了什么?我尝试用谷歌搜索该主题但失败了。

最佳答案

将您的字段更改为属性:

public class MyProduct
{
public int Id { get; set; }
public string ProductName { get; set; }
public bool selected { get; set; }

...
}

关于c# - 将 CheckBoxList 绑定(bind)到通用列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16586607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com