我正在使用
<telerik:RadComboBox>
在 Web 窗体页面中。
定义是:
<telerik:RadComboBox runat="server" ID="productList" EmptyMessage="Choose a product" />
它填充了使用 EF 检索的数据:
this.productList.DataTextField = "ProductName";
this.productList.DataValueField = "ProductId";
this.productList.DataSource = Service.GetActiveProductList();
this.productList.DataBind();
我希望下拉菜单"选择"当前产品,我是这样做的:
this.productList.SelectedIndex = this.productList.FindItemIndexByValue(this.ProductID);
我希望用户能够"清除"所选产品,以便下拉列表中没有产品是所选项目。
我正在经历的是,当我成功设置SelectedIndex时,EmptyMessage文本不会出现在列表中(我明白了,列表不为空),但是如果让我们这样说。列表中不存在产品 ID,然后确实会出现空消息,但一旦我选择一个项目,它就会消失。
简而言之,我想
- 将选定索引设置为此值。产品编号
- 始终在下拉列表中显示"选择产品"选项
this.productList.DataTextField = "ProductName";
this.productList.DataValueField = "ProductName";
尝试使用 dataValueField = ProductName 而不是 ID。因为它试图在过滤的下拉列表中查找文本,并且您的 dataValueField 是一个 int,因此它不会在列表中找到匹配项。
我认为您正在寻找默认项和 AppendDataBoundItems 属性
<telerik:RadComboBox runat="server" AppendDataBoundItems="true" ID="productList" EmptyMessage="Choose a product">
<Items>
<telerik:RadComboBoxItem Value="MyDefaultValue" Text="Choose a product" />
</Items>
</telerik:RadComboBox>