简体   繁体   English

Telerik组合框选中所有项目

[英]Telerik Combobox Check All items

I'm using C#, Asp.Net 4.0 and Telerik and I'm trying to interact with a RadComboBox. 我正在使用C#,Asp.Net 4.0和Telerik,并且正在尝试与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... 现在,它已从数据库中正确填充,但是我的所有项目都未选中...我试图通过添加以下属性“ CheckBoxes = true”来检查它们,但未成功...

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... 我觉得我在页面生命周期的错误时刻进行了操作,但是我不知道如何正确地进行操作...

Try this 尝试这个

Add an OnItemDataBound event to your RadCombobox OnItemDataBound事件添加到RadCombobox

like this 像这样

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

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. RadControls具有丰富的客户端API支持,因此您可以从客户端本身使用控件。

I tried a samll example to illustrate this scenario. 我尝试了一个samll示例来说明这种情况。 I have the following radcomboboix defined on the page: 我在页面上定义了以下radcomboboix:

<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. 我已命名组合框,将CheckBoxes设置为true,并添加了客户端事件处理程序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: 这是javascript函数:

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. 如您所见,该函数的sender参数是combobox。 I gets items out of the combobox and loop through each item and set its checked property using the set_checked(boolean) function. 我从组合框中取出项目并遍历每个项目,并使用set_checked(boolean)函数设置其checked属性。

hope you find this info useful. 希望您觉得此信息有用。 Do let me know what you think about this solution. 让我知道您对这个解决方案的看法。

Lohith (Tech Evangelist, Telerik India) Lohith(印度Telerik的技术传播者)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM