1

I am data binding a IQueryable 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?

1 Answer 1

3

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.