How to install RadComboBox with a data source in AutomaticLoadOnDemand programmatically

I am using RadComboBox . In my code, I set the selected value to RadComboBox as follows:

 public void RCB_PO_NUM_DataBound(object sender, EventArgs e) { var itemRCB_PO_NUM = RCB_PO_NUM.FindItemByText(stringPO_NUM); itemRCB_PO_NUM.Selected = true; itemRCB_PO_NUM.Value = stringPO_NUM; } 

I select a list of numbers from my database and display them in RadComboBox . So I have to use the DataBound event to get the data.

This works fine until I set the AutomaticLoadOnDemand property to true. As soon as I do this, I get the desired effect that I want using the AutomaticLoadOnDemand property, and then lose the ability to set my RadComboBox to the selected value.

I need to be able to do the same, AutomaticLoadOnDemand really helps to load items into RadComboBox to load very quickly. The code should not be in the DataBound event. I really don't care what this event is while they work. Can someone please indicate which method I use to set the AutomaticLoadOnDemand property to true or what am I doing wrong?

+7
c # telerik radcombobox rad-controls
source share
3 answers

When you use LoadOnDemand, your combobox does not bind until the user tries to expand it. Therefore, you cannot use the DataBound event.

I'm not sure what your use case is. If you want to simply display the selected item for the user, you can try using the Text property of your combobox in the Page_Load event.

 protected void Page_Load(object sender, EventArgs e) { itemRCB_PO_NUM.Text = stringPO_NUM; } 

If you really need the selected item, perhaps you can add a server with one item (sorry, I can’t check it right now)

 protected void Page_Load(object sender, EventArgs e) { itemRCB_PO_NUM.Items.Add(new RadComboBoxItem() { Value = stringPO_NUM, Text= stringPO_NUM, Selected = true }) } 

EDIT: I did a few, and it seems that the ItemDataBound event should be fired correctly:

Note. When you use the DataSourceID or DataSource properties to bind RadComboBox during on-demand automatic loading, the ItemDataBound event fires normally, which means you can use it to change the text properties and Item value, as well as to change its collection of attributes based on DataItem and etc.

So you can try using it:

 protected void RadComboBox1_ItemDataBound(object o, RadComboBoxItemEventArgs e) { DataRowView dataSourceRow = (DataRowView) e.Item.DataItem; if(e.Item.Text == stringPO_NUM) { e.Item.Selected = true; e.Item.Value = stringPO_NUM; } } 

But for me it is suspicious that on the screen that you indicated in the comments, I see that your string stringPO_NUM has a null value. I think this may be the reason why GetItemByText does not return you an element.

It would also be useful if you indicated why you need this item to select.

+2
source share

Try the OnClientLoad event and the control's JavaScript API to select the item: http://www.telerik.com/help/aspnet-ajax/combobox-client-side-radcombobox.html . Save the desired text in a hidden field or JS global variable.

The problem is that you have no items at all until the request returns, so I'm not sure if this will work. So you can try the same idea with the OnClientItemsRequested event http://www.telerik.com/help/aspnet-ajax/combobox-onclientitemsrequested.html - see if the element with the desired text is returned from the server and select it.

+1
source share

As others said, with LoadOnDemand enabled, there are no list items on the server. This is why you cannot use FindItemBy * methods - they will always return NULL.

Give more information about exactly what you want to accomplish, and then we can help.

I assume that you want to pre-populate the combo box with the text that you already have - for this you better use the client-side API, for example. in a combo download event, you can call the requestItems method ("your text", "true"), which passes the text that you already have, and the combo will make an ajax request to get the element (s) filtered by the text you pass as a parameter.

+1
source share

All Articles