How to set RadComboBox with a data source to AutomaticLoadOnDemand programmatically
Asked Answered
L

3

7

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?

Leash answered 11/7, 2014 at 19:27 Comment(3)
What is your DataSource? How do you bind data to RadComboBox?Khelat
@Khelat I use RadComboBox UI to select the table, and column that I want to display.Leash
@note What method do you use to bind data to RadComboBox? For example, SqlDataSource, EntityDataSource, Custom binding.Khelat
H
2

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.

Headwards answered 29/7, 2014 at 14:49 Comment(7)
I will give this a try and get back to you. Thank you for the response.Leash
I can't bind the data for the control on the event Page_Load, there for I can't set the value or text of the control in that event. When I try to run the code that you suggested, I receive a null value from the debugger. I found that out when I ask that question before: #24702770Leash
Could you specify which property has null value?Headwards
Hmm I am not sure why are you still searching for item by text. You will not find it because combobox does not have items yet. You don't need to bind control at all. Just set text on page load, or add dummy item. Maybe if you will tell me what you are trying to accomplish then I will be able to help you better.Headwards
I see that your stringPO_NUM is null also. What value should it have. On which event do you have this value available?Headwards
As stated in the question the works fine in the RCB_PO_NUM_DataBound event. I can set the value, text, and selected to be true just fine in that event.Leash
What I am trying to accomplish is to be able to, set the value, text, and selected to be true of the RadComboBox. At the same time I want the AutomaticLoadOnDemand property to be set to true. And of all of this to work in unison.Leash
L
1

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.

Lunate answered 15/7, 2014 at 13:16 Comment(3)
I really don't want to have to use java in order to do what I am wanting to do. Much rather do it in C#Leash
1) it is JavaSCRIPT, not JAVA 2) the whole point of having load on demand is to not load those items on the server, so they are only available on the client.Lunate
I don't want to use JAVASCRIPT (java was a typeo), but thanks anyway for the helpLeash
K
1

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.

Knapp answered 1/8, 2014 at 1:35 Comment(1)
I am using RadComboBox to select a list of numbers from a database. the As I have stated before, I don't care what event is used so that I can select the value from the RadComboBox and use the AutomaticLoadOnDemand programmatically at the same time in the codebehind.Leash

© 2022 - 2024 — McMap. All rights reserved.