vb.net 在多选 RadComboBox 中设置“选定项目”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13444846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:12:10  来源:igfitidea点击:

Set "Selected Item" in multiselect RadComboBox

asp.netvb.nettelerikradcombobox

提问by Rahul

Is there any way we can set Selected Items or Checked Items in a multiselect RadComboBox ?. I want to set value on postback from server.

有什么方法可以在多选 RadComboBox 中设置 Selected Items 或 Checked Items 吗?我想设置从服务器回发的值。

I tried following code but that works only if it is not a multiselect RadComboBox.

我尝试了以下代码,但仅当它不是多选 RadComboBox 时才有效。

Radbox1.SelectedValue = "123"

Radbox1.SelectedValue = "123"

My front end code.

我的前端代码。

<telerik:RadComboBox ID="Radbox1" runat="server" CheckBoxes="true" EnableCheckAllItemsCheckBox="true" Width="300" Height="200" AutoPostBack="True" OnSelectedIndexChanged="Radbox1_SelectedIndexChanged" />

<telerik:RadComboBox ID="Radbox1" runat="server" CheckBoxes="true" EnableCheckAllItemsCheckBox="true" Width="300" Height="200" AutoPostBack="True" OnSelectedIndexChanged="Radbox1_SelectedIndexChanged" />

I have value in Radbox1 which will be populated from database.

我在 Radbox1 中有值,它将从数据库中填充。

Thanks, Rahul

谢谢,拉胡尔

回答by KreepN

When the Radcombobox is set to allow multiple selections via the checkboxes, you must use each items checked property.

当 Radcombobox 设置为允许通过复选框进行多项选择时,您必须使用每个项目的检查属性。

I use a list here to simulate the items that I wish to have marked on postback. You could have this list pre-populated or it could even be loaded from a database:

我在这里使用一个列表来模拟我希望在回发时标记的项目。您可以预先填充此列表,甚至可以从数据库加载它:

enter image description here

在此处输入图片说明

回答by zey

protected void RadComboBox1_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
{
    if ("YourString" == e.Item.Text))
    {
        e.Item.Checked = true;
    }
}

Or

或者

protected void RadComboBox1_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
{
    List<String> yourStringList = new List<String>() {"string1","string2"};
    if (yourStringList.Contains(e.Item.Text))
    {
         e.Item.Checked = true;
    }
}

回答by Mahib

I've done something like this; Machine_Serial_Numbersis a telerik:RadComboBox;

我做过这样的事情;Machine_Serial_NumbersTelerik的:radcombobox控件;

foreach (var machine in bulletinData.Machines)
        {
            var comboItem = Machine_Serial_Numbers.FindItemByValue(machine.Id.ToString());

            if (comboItem != null)
            {
                comboItem.Checked = true;
            }                
        }

This worked for me.

这对我有用。

enter image description here

在此处输入图片说明