Reputation: 1242
I've got a dropdown/ textbox combination to enable the user to filter the data of a GridView
.
So the user would select the option 'Job Title' in the dropdown and then enter the Job title they are looking for in the textbox
. However, I want to give the user a list of suggestion of Job titles. I've been trying to do so with AutoCompleteMode
set to SuggestAppend
and a AutoCompleteSource
set to custom but this hasn't been working and I get the error that AutoCompleteMode
etc. doesn't exist in the current context.
This is the code in my TextBox1 Changed event :
TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
t=TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
addItems(DataCollection);
TextBox1.AutoCompleteCustomSource = DataCollection;
And this to set the autocomplete options:
var source = new AutoCompleteStringCollection();
source.AddRange(new string[]
{
"Marketing",
"Engineer",
"Medical",
"Insurance",
});
I'm currently filtering the data with the dropdown
list like this:
void ResultsFilter()
{
if (DropDownList1.SelectedValue.ToString() == "Name")
{
ObjectDataSource1.FilterExpression = "Name LIKE '%" + TextBox1.Text + "%' ";
}
else if (DropDownList1.SelectedValue.ToString() == "JobTitle")
{
ObjectDataSource1.FilterExpression = "JobTitle LIKE '%" + TextBox1.Text + "%' ";
}
}
Ideally I would include the auto complete only if "JobTitle" is selected. Any suggestions on how to achieve this?
Upvotes: 1
Views: 2941
Reputation: 155145
The AutocompleteMode
property is only present in WinForm's ComboBox
and TextBox
controls (and their ToolStrip
equivalents). It does not exist in ASP.NET WebForms.
Autocomplete/combobox-effects in WebForms are notoriously difficult to implement owing to the default <asp:TextBox />
coming with a lot of built-in features that get in the way.
ASP.NET WebForms doesn't come with this functionality out-of-the-box. Something like this was provided in the now deprecated ASP.NET AJAX Controls and Toolkit, but the preferred way is using jQuery now.
Fortunately jQuery makes it straightforward to add AutoComplete, though note you will need to provide a separate [WebMethod]
to provide the data that populates the autocomplete.
This page provides a thorough tutorial: http://dotnetmentors.com/aspnet/jquery-autocomplete-by-example.aspx
Note that if this is a new project, consider using ASP.NET MVC instead, which is much cleaner and makes this scenario far easier.
Upvotes: 2