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

radcombobox loses values even if none is selected

3 Answers 11 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Carlos
Top achievements
Rank 1
Carlos asked on 16 Dec 2016, 01:28 PM

Hi all,

So im having a problem with de radcombobox, i have a combobox getting values from the database, no problem there, the problem starts when i selecta a option, and then is i select the combobox, and no value is selected, and i press autside de combobox to get out of the selection the box loses the value previously selected.

 

Does anybody ave any ideas?

 

3 Answers, 1 is accepted

Sort by
0
Patrick | Senior Technical Support Engineer
Telerik team
answered on 16 Dec 2016, 05:16 PM
Hello Carlos,

There are a few ways to get a selected value from a RadComboBox.  Here are two approaches:

1.  Server-side: 

By using the SelectedIndexChanged event, you can get the value of the selected item from the EventArgs.
protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    ServerLabel.Text = "From Server-Side: You selected " + e.Text;
}

2. Client-side:

After the selected item has changed, the OnClientSelectedIndexChanged event will fire.  Use the get_Item() method and you will have access to the selected item.
<script type="text/javascript">
    function OnClientSelectedIndexChanged(sender, eventArgs) {
        var item = eventArgs.get_item();
        var label = document.getElementById("ClientLabel");
        label.textContent = "From Client-side: You selected " + item.get_text();
    }
</script>

I've attached a project which demonstrates both approaches.

I hope this helps clear things!

Regards,
Patrick
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Carlos
Top achievements
Rank 1
answered on 19 Dec 2016, 10:15 AM

Hello, 

The problem isnt getting the values from the combobox. The problem its the behavior os the combo, when clicked after a value is selected, and no value is selected.

0
Patrick | Senior Technical Support Engineer
Telerik team
answered on 19 Dec 2016, 08:53 PM
Hello Carlos,

In order to show the item has been selected when opening the RadComboBox, you will need to do some additional logic.  Here are a couple of approaches:

Client-side:
Set the background color of the selected item during the OnClientSelectedIndexChanged Event.  If there is a previously selected item, create a variable to hold the past selected item and set its background color to "transparent".  
<script type="text/javascript">
    var prevSelect;
 
    function OnClientSelectedIndexChanged(sender, eventArgs) {
        if (prevSelect != null) {
            prevSelect.get_element().style.backgroundColor = "transparent";
        
        var item = eventArgs.get_item();
        var selectedItem = sender.get_selectedItem();
        selectedItem.get_element().style.backgroundColor = "lightGreen";
        prevSelect = selectedItem;
    }
</script>

Server-side:
This approach is the same idea as Client-side except a Session State is being used to hold the previously selected index.
protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
 {
     if (Session["prevSelect"] != null)
     {
         int prevSelectIndex = (int)Session["prevSelect"];
         RadComboBox1.Items[prevSelectIndex].BackColor = System.Drawing.Color.Transparent;
     }
 
     var selectedItem = RadComboBox1.Items[RadComboBox1.SelectedIndex];
     selectedItem.BackColor = System.Drawing.Color.LightGreen;
     Session["prevSelect"] = RadComboBox1.SelectedIndex;
 }

I've attached a zip file with 2 projects demonstrating each approach.  

I hope this helps!

Regards,
Patrick
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
ComboBox
Asked by
Carlos
Top achievements
Rank 1
Answers by
Patrick | Senior Technical Support Engineer
Telerik team
Carlos
Top achievements
Rank 1
Share this question
or