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

Setting ComboBox Selected value

4 Answers 1106 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
slken
Top achievements
Rank 1
slken asked on 01 Apr 2014, 01:16 AM
I have a RadComboBox which is setup to populate the RadComboBoxItems as the user types in characters using a webservice.

Let's say the page was just loaded and the user hasn't clicked on the combobox yet. Meaning there are no values in the list yet.

How could I set the selected value of the combobox from a button click event?

I've seen code for the client side:

<telerik:RadScriptBlock runat=

 

"server"

>​​​

 

 

 

 

var

secCombo = $find(

"<%= rcbCombo.ClientID %>"

);

 

</telerik:RadScriptBlock>

secCombo.set_text(selectedName

 

 

);​​​​​​​​

 

secCombo.set_value(selectedValue);

I've also seen server side code like:

​​​​​​​​​​

 

 

 

 

RadComboBoxItem

item = rcb_securities_list.FindItemByText(selectedName);

 

item.Selected =

 

 

true

;

 

But I think neither is working because either EnableAutomaticLoadOnDemand is enabled or due to the control has no values loaded yet.

How can I set the value?

 

 

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 01 Apr 2014, 06:03 AM
Hi slken,

RadComboBox items loaded on demand using the ItemsRequested event handler or WebService do not exist on the server and cannot be accessed using the server-side FindItemByText / Value methodsSelectedItem and SelectedIndex properties are always Null / Nothing.This is needed for speed (otherwise the combobox will not be that responsive upon each keypress if state information and ViewState were persisted). The properties that are accessible on the server are: Text and SelectedValue.

Thanks,
Shinu.
0
slken
Top achievements
Rank 1
answered on 01 Apr 2014, 04:42 PM
Thanks for the insightful information.

I'm still unsure how I can approach attaining what I need to do.

Along with the RadComboBox, I have a jQuery modal form which allows users to search for values. Upon selection on the modal form, I would like to set the value of the RadComboBox using the selected value.

How can I do that?
0
Shinu
Top achievements
Rank 2
answered on 02 Apr 2014, 09:22 AM
Hi slken,

Please have a look into the following sample code snippet to achieve your scenario.

ASPX:
<telerik:RadTextBox ID="RadTextBox1" runat="server">
</telerik:RadTextBox>
<telerik:RadButton ID="RadButton1" runat="server" Text="Click" AutoPostBack="false"
    OnClientClicked="OnClientClicked">
</telerik:RadButton>
<telerik:RadComboBox ID="RadComboBox1" runat="server" Width="250" Height="150"  EnableLoadOnDemand="true" ShowMoreResultsBox="true" EnableVirtualScrolling="true" OnClientItemsRequested="OnClientItemsRequested">
    <WebServiceSettings Method="GetCompanyNames" Path="Products.asmx" />
</telerik:RadComboBox>

JavaScript:
<script type="text/javascript">
    var comboItem;
    function OnClientClicked(sender, args) {
        var textbox = $find("<%=RadTextBox1.ClientID %>");
        comboItem = textbox.get_value();
        var combo = $find("<%=RadComboBox1.ClientID %>");
        combo.requestItems(comboItem);
    }
    function OnClientItemsRequested(sender, eventArgs) {
        sender.findItemByText(comboItem).select();
    }
</script>

Thanks,
Shinu.
0
Shinu
Top achievements
Rank 2
answered on 02 Apr 2014, 10:04 AM
Hi slken,

In my previous post the function "OnClientItemsRequested" have some issues. So please try the following JavaScript to achieve your scenario.

JavaScript:
function OnClientItemsRequested(sender, args) {
    var item = sender.findItemByText(comboItem);
    if (item != null) {
        item.select();
    }
 
}

Thanks,
Shinu.
Tags
ComboBox
Asked by
slken
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
slken
Top achievements
Rank 1
Share this question
or