View AutoComplete list on ASP.Net Ajax AutoCompleteExtender TextBox focus using C# and VB.Net

nauna
 
on Sep 07, 2021 10:42 PM
Sample_114626.zip
1512 Views

hello,

i am using this ajax auto complete AJAX AutoCompleteExtender Example in ASP.Net

it works well, i want when user click on textbox it shows 10 auto suggestion on textbox click then if user needs to search user can type in textbox to search

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 08, 2021 05:15 AM

Hi nauna,

You need to set the MinimumPrefixLength="0" to display list on click of TextBox.

Refer below example.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

HTML

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" ServiceMethod="SearchCustomers"
    MinimumPrefixLength="0" CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
    TargetControlID="txtContactsSearch" FirstRowSelected="false">
</cc1:AutoCompleteExtender>

Namespaces

C#

using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;

VB.Net

Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.Services

Code

C#

[WebMethod]
public static List<string> SearchCustomers(string prefixText, int count)
{
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select ContactName from Customers where ContactName like @SearchText + '%'";
            cmd.Parameters.AddWithValue("@SearchText", prefixText);
            cmd.Connection = conn;
            conn.Open();
            List<string> customers = new List<string>();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(sdr["ContactName"].ToString());
                }
            }
            conn.Close();

            return customers;
        }
    }
}

VB.Net

<WebMethod()>
Public Shared Function SearchCustomers(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
    Using conn As SqlConnection = New SqlConnection()
        conn.ConnectionString = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using cmd As SqlCommand = New SqlCommand()
            cmd.CommandText = "select ContactName from Customers where ContactName like @SearchText + '%'"
            cmd.Parameters.AddWithValue("@SearchText", prefixText)
            cmd.Connection = conn
            conn.Open()
            Dim customers As List(Of String) = New List(Of String)()
            Using sdr As SqlDataReader = cmd.ExecuteReader()
                While sdr.Read()
                    customers.Add(sdr("ContactName").ToString())
                End While
            End Using
            conn.Close()

            Return customers
        End Using
    End Using
End Function

Screenshot