Pregunta

I'm using C#, Asp.Net 4.0 and Telerik and I'm trying to interact with a RadComboBox.

I'm populating it with an entity data source like this :

<RadComboBox ID="cbMyCombo" runat="server" AutoPostBack="true" CheckBoxes="true" DataSourceID="edsMySource" DataTextField="Name" DataValueField="Number">

Now, it's populated properly from the database but all my items are unchecked... I tried unsuccessfully to check them by adding the following property "CheckBoxes=true" but it's not working...

I tried to change it in the code behind like this :

protected override void OnLoad(EventArgs e)
{
  base.OnLoad(e);

  for (int i = 0; i < cbMyCombo.Items.Count; i++)
    {
      cbMyCombo.Items[i].Checked = true;
    }
  }
}

Nice try, no cigar...

I have the feeling that I'm doing it at the wrong moment in the page life cycle but I don't know how to make it properly...

¿Fue útil?

Solución

Try this

Add an OnItemDataBound event to your RadCombobox

like this

protected void RadComboBox1_ItemDataBound(object o, RadComboBoxItemEventArgs e) 
{ 
    e.Item.Checked = true;
}

Otros consejos

There is another way to handle this scenario. If all you want is - all the items in the combo box to be checked - then you can do so on the client side too. RadControls have rich client side API support so you can play around with the control from client side itself.

I tried a samll example to illustrate this scenario. I have the following radcomboboix defined on the page:

<telerik:RadComboBox runat="server" CheckBoxes="true" OnClientLoad="clientLoadHandler"
        ID="radCombo"></telerik:RadComboBox>

I have named the combobox, set the CheckBoxes to true and I have added a client side event handler OnClientLoad. In this example i am binding the data source from the server as below:

 List<string> colors = new List<string>
        {
            "Violet",
            "Indigo",
            "Blue",
            "Green",
            "Yellow",
            "Orange",
            "Red"
        };
        radCombo.DataSource = colors;
        radCombo.DataBind();

Here is the javascript function:

function clientLoadHandler(sender) {
            var combo = sender;
            var items = combo.get_items();
            var itemCount = items.get_count()
            for (var counter = 0; counter < itemCount; counter++) {
                var item = items.getItem(counter);
                item.set_checked(true)
            }
        }

As you can see, the sender parameter of the function is the combobox. I gets items out of the combobox and loop through each item and set its checked property using the set_checked(boolean) function.

hope you find this info useful. Do let me know what you think about this solution.

Lohith (Tech Evangelist, Telerik India)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top