实体类里面有对象属性,现在数据库里面取出该实体类对象集合,绑定到dropdownlist, 通过DataTextField ,DataValueField 属性绑定,但是如果直接写实体类名绑定无效,请高手指点。
要绑定的字段是对象类型
DropDownList2.DataTextField = "Shop.shopName";
这样会报错.......
只需要将数据源中相应的字段给DataTextField 即可。如
DropDownList2.DataTextField = "shopName"; 而不是
DropDownList2.DataTextField = "Shop.shopName";
IList<Shop> lst = _dal.GetAll();
DropDownList2.DataSource = lst;
DropDownList2.DataValueField = "ShopiId";
DropDownList2.DataTextField = "ShopName";
DropDownList2.DataBind();
this.DropDownList2.DataSource =bll.GetAllShop();//调用查询表中的方法
DropDownList2.DataValueField = "id";
DropDownList2.DataTextField = "name";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, new ListItem("--请选择--", "0"));
这样就OK了
返回的集合对象必须是泛型的,才可使用DropDownList2.DataTextField = "shopName";
来源:英超直播