如果搜索后未找到项目,RadComboBox将显示消息

本文关键字:RadComboBox 显示 消息 项目 搜索 如果 | 更新日期: 2023-09-27 18:25:37

我有一个RadGrid,其中一列是GridTemplateColumn,它有一个加载某些项目的RadComboBox(编辑模式设置为"弹出")。我想要的是,如果在RadComboBox中搜索项目时,没有找到任何项目,那么给用户一个添加新项目的选项。目前,仅出于测试目的,我希望能够在未找到项目的情况下显示消息。这就是我迄今为止所尝试的。

RadGrid中的我的RadComboBox定义如下:

 <EditItemTemplate>
    <telerik:RadComboBox runat="server" ID="Product_PKRadComboBox" 
    ShowDropDownOnTextboxClick="false" ShowMoreResultsBox="true" EnableVirtualScrolling="true"
    EnableLoadOnDemand="true" EnableAutomaticLoadOnDemand="true" ItemsPerRequest="10"
    OnItemsRequested="Product_PKRadComboBox_ItemsRequested" AllowCustomText="true"
    Filter="StartsWith" DataSourceID="SqlProducts" DataTextField="ProductCode"
    DataValueField="Product_PK"></telerik:RadComboBox>
 </EditItemTemplate>

因此,我在"OnItemsRequested"事件中检查我的逻辑,如下所示:

 protected void Product_PKRadComboBox_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
    {
        //RadComboBox combo = (RadComboBox)sender;
        if (e.EndOfItems && e.NumberOfItems==0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "testMessage", "alert('Product Not Found. Do you want to add a Custom Product?');", true);
            //Page.ClientScript.RegisterStartupScript(typeof(Page), "some_name", "if(confirm('here the message')==false)return false;");
        }
    }

我尝试了IF语句中的两行代码(检查用户在RadComboBox中键入的内容是否存在,如果不返回任何项,则显示消息),但似乎都不起作用。我在调试模式下尝试了同样的操作,并在IF语句中的行上设置了一个断点,它确实执行了,但我看不到警报。

如果搜索后未找到项目,RadComboBox将显示消息

我刚刚做了类似的事情,我的解决方案似乎运行得很好。

基本上在ItemsRequested中,一旦我知道没有找到匹配项,我就会手动添加RadComboBoxItem。给它一个有意义的文本,比如"添加一个新产品…",并给它一种独特的风格,以将其与实际结果区分开来。

类似这样的东西:

protected void Product_PKRadComboBox_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
    if (e.EndOfItems && e.NumberOfItems==0)
    {
        var addItem = new RadComboBoxItem("Add a new product...", "addnewproduct");
        addItem.ToolTip = "Click to create a new product...";
        addItem.CssClass = "UseSpecialCSSStyling";
        Product_PKRadComboBox.Items.Add(addItem);
    }
}

然后,当选择"addnewproduct"项时,您需要处理SelectedIndexChanged事件。请确保将组合框的AutoPostBack设置为"true"。

protected void Product_PKRadComboBox_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{            
    if (!string.IsNullOrEmpty(e.Value) && e.Value.Equals("addnewproduct"))
    {
        // do whatever you have to do to add a new product
    }       
} 

您可以使用RadWindow显示一个带有"是"answers"取消"按钮的确认框,上面写着"您确定要添加新产品吗?"。更进一步,显示用户在RadWindow的文本框中键入的搜索文本。通过这种方式,用户可以在添加新项目之前修改文本。

例如,用户可以键入"水瓶"来搜索产品。未找到结果,因此用户单击"添加新产品…"项目。显示确认框,然后用户可以将"水瓶"更改为"600毫升绿色耐用水瓶",然后单击"是"以实际添加产品。