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?

nate
  • 1,418
  • 5
  • 34
  • 73
  • What is your DataSource? How do you bind data to RadComboBox? – Win Jul 29 '14 at 15:35
  • @Win I use RadComboBox UI to select the table, and column that I want to display. – nate Jul 29 '14 at 18:14
  • @note What method do you use to bind data to RadComboBox? For example, SqlDataSource, EntityDataSource, Custom binding. – Win Jul 29 '14 at 18:21

3 Answers3

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.

Machet
  • 1,090
  • 8
  • 17
  • I will give this a try and get back to you. Thank you for the response. – nate Jul 29 '14 at 18:12
  • 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: http://stackoverflow.com/questions/24702270/how-to-set-a-radcombobox-that-is-using-a-data-source-to-a-selected-value-in-code – nate Jul 30 '14 at 03:29
  • Could you specify which property has null value? – Machet Jul 30 '14 at 06:57
  • 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. – Machet Jul 30 '14 at 21:51
  • I see that your stringPO_NUM is null also. What value should it have. On which event do you have this value available? – Machet Jul 30 '14 at 22:02
  • 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. – nate Jul 31 '14 at 16:12
  • 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. – nate Jul 31 '14 at 16:56
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.

rdmptn
  • 5,413
  • 1
  • 16
  • 29
  • 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# – nate Jul 28 '14 at 17:33
  • 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. – rdmptn Jul 29 '14 at 08:42
  • I don't want to use JAVASCRIPT (java was a typeo), but thanks anyway for the help – nate Jul 29 '14 at 18:09
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.

Veselin Vasilev
  • 3,698
  • 1
  • 15
  • 21
  • 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. – nate Aug 01 '14 at 13:49