This is a migrated thread and some comments may be shown as answers.

SelectedIndex depending on datasource value

5 Answers 108 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Datamex
Top achievements
Rank 2
Datamex asked on 10 Mar 2011, 09:50 AM

Well, I have seen similar problems and corresponding solutions on the forums, but none of those was applicable for this problem. I already have tried and searched almost any given solution on the forums here and around the internet, but unfortunately to no avail.

So, what exactly is the problem? I have got a RadComboBox which has an ObjectDataSource. This ComboBox has been placed in an EditItemTemplate within a RadListView. I want this RadComboBox to display the name of the currently selected customer as SelectedIndex (SelectedValue or Text). Items are filled based upon data out of an XmlDataSource object. The ObjectDataSource contains all possible customers.

Let's say I have inserted a new item with customer: "Customer1", but I'd like to edit this item. When you go into the EditItemTemplate view, I want to have the RadComboBox select (SelectedIndex/Value or Text) "Customer1" (which is based upon the XmlDataSource) from the ObjectDataSource.

My RadComboBox and ObjectDataSource look like:

<telerik:RadComboBox ID="RadComboBox1" 
    runat="server" 
    AllowCustomText="false" 
    DataSourceID="ObjectDataSource1" 
    DataTextField="Name" 
    DataValueField="Name"
    DropDownWidth="128px"
    EmptyMessage="-Customer-" 
    EnableAutomaticLoadOnDemand="true"
    EnableVirtualScrolling="true" 
    Filter="Contains"
    Height="150px" 
    ItemsPerRequest="10"
    MarkFirstMatch="true"
    MinFilterLength="2"
    ShowMoreResultsBox="true"
    Width="128px">
</telerik:RadComboBox>
<asp:ObjectDataSource ID="ObjectDataSource1"
    runat="server" 
    SelectMethod="GetCustomers" 
    TypeName="RadControlsWebApp1.Customers">
</asp:ObjectDataSource>
The SelectMethod="GetCustomers" returns a List<Customer> object. A Customer is just a simple class which holds a (string) name. You could actually just use a List<string> for the same purpose.

The XML file (XmlDataSource object) looks like:
<?xml version="1.0" encoding="utf-8"?>
<Activities>
  <Activity Customer="Customer1" Project="Project1" />
  <Activity Customer="Customer1" Project="Project2" />
  <Activity Customer="Customer2" Project="Project2" />
  <Activity Customer="Customer3" Project="Project3" />
</Activities>

I have tried to set the Text property of the RadComboBox as follows: Text='<%# Bind("Customer") %>' and setting the RadComboBox.SelectedIndex or RadComboBox.SelectedValue properties programmatically, but as I already mentioned, without results. Perhaps I tried to set the properties in the wrong event(s).

5 Answers, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 15 Mar 2011, 09:38 PM
Hi Datamex,

Setting the Text property in the way you have tried would not help much as it will not actually change the selection in the RadComboBox. The SelectedIndex/Value approach would work and the most appropriate place to do this is the DataBound event of the RCB. Please try using it and let me know how it goes.

Best wishes,
Simon
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Datamex
Top achievements
Rank 2
answered on 16 Mar 2011, 10:59 AM
Hi Simon,

Thanks for your reply, but I guess you have overlooked the properties of the RCB. As you can see the data retrieval actually occurs when you click the dropdown button (ItemsPerRequest="10" and ShowMoreResultsBox="true"). So the DataBound event fires when you click the RCB, but that's too late and it's possible the desired item hasn't been retrieved yet. Let's say you have 20 items, ItemsPerRequest="10" and you want the RCB's SelectedValue to have a value of the last 10 items, then you get the exception 'value out of range'. In my case the RCB should have SelectedValue filled when it's displayed.

Looking forward to your response.

Regards,
Datamex
0
Simon
Telerik team
answered on 17 Mar 2011, 06:27 PM
Hello Datamex,

Thank you for getting back to me and further explaining your requirement.

We have a demo showing a very similar implementation, so please take a look at it here. Basically the load on demand RadComboBox is pre-populated with the respective Item that has been previously inserted in the same column.

Please let me know if this helps.

Regards,
Simon
the Telerik team
0
Datamex
Top achievements
Rank 2
answered on 18 Mar 2011, 09:00 AM
Hi Simon,

Problem solved!

The demo cleared a lot of things up for me. Fortunately I was able to get the desired result, that is the SelectedValue of the RCB filled with the preselected item value. I already had this ItemDataBound event filled, but I did not realize it was working properly. For example: if you have "customer1", "customer10" and "customer100" in your datasource and you have an item with value "customer1" you would assume (from my point of view) it would show "customer10" and "customer100" too in the RCB. On the other hand, in an autocomplete RCB this is not standard behaviour, so the result I already had turned out as a result I wished for at first.

Thanks for the explanation and quick support!

Regards,
Datamex

0
Simon
Telerik team
answered on 18 Mar 2011, 09:59 AM
Hi Datamex,

It is good that the demo has been helpful.

Please feel free to write to us again if you need assistance with products.

Greetings,
Simon
the Telerik team
Tags
ComboBox
Asked by
Datamex
Top achievements
Rank 2
Answers by
Simon
Telerik team
Datamex
Top achievements
Rank 2
Share this question
or