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.
精彩评论