This is a migrated thread and some comments may be shown as answers.

Just display No Matches in e.Message and no empty combo box item

4 Answers 217 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Jinisha
Top achievements
Rank 1
Jinisha asked on 28 Dec 2012, 09:15 PM
I have a RadCombobox where in the first name textbox if I type a letter...it shows me list of users that begin with that letter.

<telerik:RadComboBox Visible="false" Skin="Vista" TabIndex="10" ID="rcbLastName" runat="server"
                                                           AllowCustomText="true" Width="200px" ShowToogleImage="false" EnableOverlay="true" ShowDropDownOnTextboxClick="false" DropDownWidth="200px" MaxLength="20" CausesValidation="false"
                                                           EnableLoadOnDemand="true" EnableScreenBoundaryDetection="false" ExpandDirection="Up" EnableVirtualScrolling="true" ShowMoreResultsBox="true"
                                                           AutoPostBack="True" OnClientItemsRequesting="OnClientItemsRequesting" OnClientDropDownOpening="OnClientDropDownOpening" OnItemsRequested="rcbLastName_ItemsRequested" OnSelectedIndexChanged="rcbBarNumber_SelectedIndexChanged"
                                                           ItemsPerRequest="5">

I have server side code rcbFirstName_itemsrequested that pulls the data out of the db and displays it. 
if (!String.IsNullOrEmpty(e.Text))
            {
                rcbLastName.ClearSelection();
                NameValueCollection nvc = new NameValueCollection();
                nvc.Add("LastName", e.Text);
  
                DataTable accounts = DB.ExecuteDataTable("usp_AccountsGetByLastName", nvc);
                int itemOffset = e.NumberOfItems;
                int endOffset = Math.Min(itemOffset + rcbLastName.ItemsPerRequest, accounts.DefaultView.Count);
                e.EndOfItems = endOffset == accounts.DefaultView.Count;
  
                for (int i = itemOffset; i < endOffset && i < accounts.DefaultView.Count; i++)
                {
                    string data = accounts.DefaultView[i]["FullName"].ToString();
                    if (!String.IsNullOrEmpty(accounts.DefaultView[i]["Address1"].ToString()))
                    {
                        data += " - " + accounts.DefaultView[i]["Address1"].ToString() + " ";
                        if (!String.IsNullOrEmpty(accounts.DefaultView[i]["Address2"].ToString()))
                            data += accounts.DefaultView[i]["Address2"].ToString() + " ";
                        data += accounts.DefaultView[i]["City"].ToString() + " " + accounts.DefaultView[i]["State"].ToString() + " " + accounts.DefaultView[i]["Zip"].ToString();
                    }
                    rcbLastName.Items.Add(new RadComboBoxItem(data, accounts.DefaultView[i]["AccountID"].ToString()));
                }
                if (accounts.DefaultView.Count <= 0)
                    e.Message = "No matches";
                else
                    e.Message = String.Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", endOffset, accounts.DefaultView.Count);
            }

The drop down is working fine. However I do not understand how to get rid of the empty Combo box item if there are no matches. Also, how to get rid of the down arrow if there are no matches. 

What do I need to do if I don't want to show the drop down if there are no matches.

Please advice!

Thanks!

4 Answers, 1 is accepted

Sort by
0
Jinisha
Top achievements
Rank 1
answered on 31 Dec 2012, 09:19 PM
function OnClientItemsRequesting(sender, eventArgs) {
        sender.hideDropDown();
}
function OnClientItemsRequested(sender, eventArgs) {
    if (sender.get_items().get_count() > 0) {
        sender.showDropDown();
    }
}
function OnClientDropDownOpening(sender, eventArgs) {   
    if (sender.get_items().get_count() == 0)
       eventArgs.set_cancel(true);  
}
I added the above javascript and modified the aspx with the following change.

<telerik:RadComboBox Visible="false" Skin="Vista" TabIndex="10" ID="rcbLastName" runat="server"
                                                           AllowCustomText="true" Width="200px" Filter="StartsWith" HighlightTemplatedItems="true" ShowToggleImage="false" EnableOverlay="true" ShowDropDownOnTextboxClick="false" DropDownWidth="350px" MaxLength="20" CausesValidation="false"
                                                           EnableLoadOnDemand="true" EnableScreenBoundaryDetection="false" ExpandDirection="Up" EnableVirtualScrolling="true" ShowMoreResultsBox="true"
                                                           AutoPostBack="True" OnClientItemsRequesting="OnClientItemsRequesting" OnClientItemsRequested="OnClientItemsRequested" OnClientDropDownOpening="OnClientDropDownOpening" OnItemsRequested="rcbLastName_ItemsRequested" OnSelectedIndexChanged="rcbAccountNumber_SelectedIndexChanged"
                                                           ItemsPerRequest="5">                                                                                                                           
                                                       </telerik:RadComboBox>

the above works...the problem is when there is a match suppose i type a..an then continue typing the letters on a slow speed...the drop down closes opens while I type each letter...which makes it very uneasy to look at...what should do to leave the drop down menu open if the letters match and the user is typing?
0
Nencho
Telerik team
answered on 03 Jan 2013, 12:27 PM
Hello Jinisha,

The DropDown of the RadComboBox closes and opens again, because the showDropDown() method is executed each time the items are requested. I can suggest  that you hide the DropDown in the OnClientItemsRequesting event when the items' count is 0. Thus you could prevent the unnecessary opening of the dropdown each time the ItemsRequested event is fired. In addition, please note that the AllowCustomText property is set to True by default, when the LoadOnDemand mechanism is enabled, so you do not need to explicitly set it.

All the best,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Jinisha
Top achievements
Rank 1
answered on 03 Jan 2013, 03:45 PM
I tried the following but still running into the same issue

function OnClientItemsRequesting(sender, eventArgs) {
    if (sender.get_items().get_count() == 0)
        sender.hideDropDown();  
}
0
Nencho
Telerik team
answered on 08 Jan 2013, 02:33 PM
Hello Jinisha,

Could you try the following approach and let us know if it works properly at your end:

function OnClientItemsRequested(sender, eventArgs) {
           if (sender.get_items().get_count() > 0 && !sender.get_dropDownVisible()) {
               sender.showDropDown();
           }
           else if (sender.get_items().get_count() == 0) {
               sender.hideDropDown();
           }
       }
       function OnClientDropDownOpening(sender, eventArgs) {
           if (sender.get_items().get_count() == 0) {
               eventArgs.set_cancel(true);
           }
       }



All the best,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
ComboBox
Asked by
Jinisha
Top achievements
Rank 1
Answers by
Jinisha
Top achievements
Rank 1
Nencho
Telerik team
Share this question
or