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

Using 'enter' to search when combobox has serverside filtering

1 Answer 95 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
staron
Top achievements
Rank 1
staron asked on 29 Jan 2009, 08:29 AM
Hello,
I'm having a problem that I was hoping you guys could help me with. As the sujects says, I have a combobox with serverside filtering. To reduce db strain, it should only do itemrequest when users press enter. Any tip on how to solve this?

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 29 Jan 2009, 02:20 PM
Hi Staron,

I guess you are using the logic for populating the RadComboBox in ItemRequested event handler. And you want to populate the RadComboBox only when pressing Enter key from keyboard. Here I have written the code for firing the ItemRequested event when pressing Enter key. You can try your own logic to poulate the RadComboBox in ItemRequested event.

ASPX:
<telerik:RadComboBox ID="RadComboBox1" runat="server" HighlightTemplatedItems="true" EnableLoadOnDemand="false" Skin="Default" AllowCustomText="true" OnClientDropDownOpening="OnClientDropDownOpening" CloseDropDownOnBlur="false" OnClientKeyPressing="OnClientKeyPressing" onitemsrequested="RadComboBox1_ItemsRequested">  
</telerik:RadComboBox> 

JavaScript:
<script type="text/javascript">  
function OnClientDropDownOpening(sender, eventArgs)  
{    
    if (sender.get_text().length < 1)  
    {  
        eventArgs.set_cancel(true);  
    }  
    else   
    {  
        sender.requestItems(sender.get_text(),false);  
    }  
}  
function OnClientKeyPressing(sender, eventArgs)   
{  
    if (eventArgs.get_domEvent().keyCode == 13)   
    {  
        sender.set_closeDropDownOnBlur(false);  
        sender.showDropDown();  
    }  
}  
</script> 

CS:
protected void RadComboBox1_ItemsRequested(object o, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e)  
{  
    string sql = "SELECT * from Employees WHERE LastName LIKE '" + e.Text + "%'";  
    SqlDataAdapter adapter = new SqlDataAdapter(sql, ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);  
    DataTable data = new DataTable();  
    adapter.Fill(data);  
    for (int i = 0; i < data.Rows.Count; i++)  
    {  
        RadComboBox1.Items.Add(new RadComboBoxItem(data.Rows[i]["LastName"].ToString(), data.Rows[i]["LastName"].ToString()));  
    }          

Thanks,
Princy.
Tags
ComboBox
Asked by
staron
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or