2014-10-06 42 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毫升」。

相關問題