0

我有一个 RadGrid,其中我的一个列是 GridTemplateColumn,它有一个加载一些项目的 RadComboBox(编辑模式设置为“PopUp”)。我想要的是,如果在 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 语句中的行上设置了一个断点,它实际上确实执行了,但我看不到警报。

4

1 回答 1

0

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

基本上在 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 显示一个确认框,上面写着“您确定要添加新产品吗?” 带有是和取消按钮。通过显示用户在 RadWindow 内的文本框中输入的搜索文本更进一步。这样,用户可以在添加新项目之前修改文本。

例如,用户可以键入“水瓶”来搜索产品。未找到任何结果,因此用户单击“添加新产品...”项。确认框出现,然后用户可以将“水瓶”更改为“绿色耐用水瓶 600ml”,然后单击“是”以实际添加产品。

于 2015-03-16T00:24:27.237 回答