2014-10-06 31 views
0

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

回答

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内的文本框中输入的搜索文本。这样用户可以在添加新项目之前修改文本。

例如,用户可以键入“水瓶”来搜索产品。没有找到结果,因此用户点击“添加新产品...”项目。确认框出现后,用户可以在点击是以实际添加产品之前将“水瓶”更改为“绿色耐用水瓶600毫升”。

相关问题