开发者

ListBox with Entity Framework

开发者 https://www.devze.com 2023-03-12 07:11 出处:网络
I am data binding a I开发者_如何学GoQueryable collection of Suppliers to a listbox as follows in my page load event...

I am data binding a I开发者_如何学GoQueryable collection of Suppliers to a listbox as follows in my page load event...

        SupplierRepository sr = SupplierRepository.GetInstance();
        lbSuppliers.DataSource = sr.FindAll();
        lbSuppliers.DataTextField = "SupplierName";
        lbSuppliers.DataValueField = "SupplierID";
        lbSuppliers.DataBind();

But for some reason when I try to get the selectedvalue from the list box as follows it's returning null, even though I can see the values in the html source view at runtime.

lbSuppliers.SelectedValue.ToString();

Seems a very basic problem, but I am stuck. Is this because I am using a EF collection for the data source?


Are you binding every PageLoad? If you are, then the SelectedItem will disappear.

Try wrapping the DataBinding code in an If (!IsPostBack) block.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号