Hello !

Faced such a problem: I can not find a solution how to make AutoComplete in ASP .NET (live search) without jQuery and without Ajax .

That is, in WinForms it would be like this:

 textBox1.AutoCompleteCustomSource = source; textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend; textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; 

Data in TextBox I want to take from String[] . Maybe someone will throw tutorial.

Thank you!

    2 answers 2

    On the web, if without loading (i.e. using ajax), you have to immediately mark all the options on the web page, for example, in the js variable and also the js script can be loaded in the input field. Or pre-create html-list of all options and use it with the help of datalist:

     <!DOCTYPE html> <html> <head> </head> <body> <input type="text" id="txtAutoComplete" list="languageList"/> <datalist id="languageList"> <option value="HTML" /> <option value="CSS" /> <option value="JavaScript" /> <option value="SQL" /> <option value="PHP" /> <option value="jQuery" /> <option value="Bootstrap" /> <option value="Angular" /> <option value="ASP.NET" /> <option value="XML" /> </datalist> </body> </html> 

    A source

    Example of loading from the array Strings []: HTML code ASP.Net pages:

     <asp:TextBox ID="tbxInput" runat="server" list="myList"></asp:TextBox> <asp:Label ID="lblmyList" runat="server" Text=""></asp:Label> 

    Function in the page load code:

     protected void Page_Load(object sender, EventArgs e) { String[] myArray = new String[] { "один", "два", "три", "дважды" }; string myList = "<datalist id='myList'>"; foreach (string str in myArray) { myList += "<option value='" + str + "' />"; } myList += "</datalist>"; lblmyList.Text = myList; } 

    The result of the example:

    result

    If you need a load, but there is no desire to write an ajax-loader yourself, you can use the AutoComplete component from the ajaxControlToolkit library . Example:

     <ajaxToolkit:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="myTextBox" ServiceMethod="GetCompletionList" ServicePath="AutoComplete.asmx" MinimumPrefixLength="2" CompletionInterval="1000" EnableCaching="true" CompletionSetCount="20" CompletionListCssClass="autocomplete_completionListElement" CompletionListItemCssClass="autocomplete_listItem" CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem" DelimiterCharacters=";, :"> <Animations> <OnShow> ... </OnShow> <OnHide> ... </OnHide> </Animations> </ajaxToolkit:AutoCompleteExtender> 

    In the code (C #), you will need to write a load handler:

     [System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod] public string[] GetCompletionList(string prefixText, int count) { ... 
    • For some reason, I constantly had a compiler swearing at this library, I constantly didn’t like something. So I decided to find a simple way. Since the data is taken from the String array. - kxko
    • Is the array large? Does it make sense to immediately upload it to the page? - Ella Svetlaya
    • @kxxko If without a library, then use the first option. In the code when loading the page, fill in the datalist from your array and that's it. - Ella Svetlaya
    • @kxxko added an example of loading from the array to the response. - Ella Svetlaya
    • Thanks a lot ! - kxko

    In html there is a tag called datalist. You specify in it a list of necessary values, and then write the identifier of this datalist in the list attribute of your text field. After that, your text field works like a drop-down list, the content of which is taken from a datalist, which you can fill with anything. Example:

     <input list="mylist"> <datalist id="mylist"> <option value="First"> <option value="Second"> <option value="Third"> </datalist> 

    The same jsfiddle example . Using ajax is not necessary here (although if you prefer, you can fasten the filling of the datalist with a request), you can fill it when rendering the page on the server.

    Example for ASP.NET:

     <input list="mylist"> <datalist id="mylist"> <% foreach (var str in Strings) {%> <option><%= str%></option> <% } %> </datalist> 

    Where Strings is your array of strings. For example:

      public string[] Strings { get { return new[] { "First", "Second", "Third", "Fourth" }; } } 
    • And do not tell me how you can fill in the value of the values ​​from the array? Is it possible to do this not in html, but in C #? - kxko
    • @kxxko added an example for ASP.NET - DreamChild
    • Thanks, helped! Have a nice day =) - kxko