Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set RadComboBox with a data source to AutomaticLoadOnDemand programmatically

I am using a RadComboBox. In my code I set the selected value to the RadComboBox like so:

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 am selecting a list of numbers from my database, and displaying them in the RadComboBox. So I have to use the DataBound event to get the data.

That works great till I set the AutomaticLoadOnDemand property to true. Once I do that, I get the desired effect that I want with the AutomaticLoadOnDemand property, and then lose the ability to set my RadComboBox to a selected value.

I need to be able to do both, the AutomaticLoadOnDemand really help the loading of the items in the RadComboBox to load really fast. The code doesn't have to be in the DataBound event. I really don't care what event it is in, just as long as both work. Can some please tell what method I use to set the AutomaticLoadOnDemand property to true, or what I am doing wrong?

like image 779
nate Avatar asked Jul 11 '14 19:07

nate


3 Answers

When you use LoadOnDemand then your combobox isn't bound until user try to expand it. So you can't use DataBound event.

I am not sure what is your use case. If you want to just display selected item to user then you can to try Text property of your combobox in Page_Load event.

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

If you really need selected item then maybe you can add single item server side (sorry I can't test 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 some research and it seems that ItemDataBound event should be fired correctly:

Note: When you use the DataSourceID or DataSource properties to bind RadComboBox during automatic Load On Demand the ItemDataBound event fires normally, which means that you can use it to change the Item's Text and Value properties as well as modify its Attributes collection based on the DataItem, etc.

So you can try to use 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 what is suspicious to me is that on screen that you provided in the comments I can see that your string stringPO_NUM has null value. I think that this may be the reason why GetItemByText doesn't return an item to you.

Also it would be helpfull if you would specify why do you need this item to be selected.

like image 126
Machet Avatar answered Oct 11 '22 14:10

Machet


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

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

like image 30
rdmptn Avatar answered Oct 11 '22 13:10

rdmptn


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

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

I guess you want to pre-populate the combobox with the text that you already have - for this you better use the client-side API, e.g. on combo load event you can invoke the requestItems("your text", true) method passing the text that you already have and the combo will make an ajax request to get the item(s) filtered by the text you pass as a parameter.

like image 33
Veselin Vasilev Avatar answered Oct 11 '22 12:10

Veselin Vasilev