How to set a RadComboBox that is using a data sour

2019-09-07 00:53发布

问题:

I have a RadComboBox that is bounded by a datasource. I set the datacource to select from a the database using a select query. Run I run the project, I get a complete list of items in the RadComboBox.

I want to set the RadComboBox to a selected value, or index in the codebehind. So the RadComboBox will have a value in it once the page is load, instead of it being empty.

I have tried to do this with the code like so:

RadComboBoxItem item = RCB_PO_NUM.FindItemByText("2000");
item.Selected = true;

But I get a null value in the debugger once I run the program. I have tried to put the code in the Page_Load , Page_LoadComplete, and Page_Init methods. It still comes back as an null value. Can some please tell me where I should put the code, so that it will not return a null value?

回答1:

You can add a property on your viewModel for the selected item and bind the radion button's SelectedItem property to it.

Alternatively try this code in the RCB_PO_NUM.DataBound event handler.

RadComboBoxItem item = RCB_PO_NUM.FindItemByText("2000");
RCB_PO_NUM.SelectedItem = item;