dropdownlist绑定对象属性

实体类里面有对象属性,现在数据库里面取出该实体类对象集合,绑定到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";

来源:英超直播

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值