Hi nauna,
If user type 3 character then disables the TextBox so that user will not be able to type anything.
Then re-enable the TextBox either after 2 second or on autocomplete item selection.
Refer below code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:TextBox ID="txtContactsSearch" runat="server" onkeyup="return StopType(this)"></asp:TextBox>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" EnableCaching="false"
MinimumPrefixLength="1" TargetControlID="txtContactsSearch" ServiceMethod="SearchCustomers"
CompletionInterval="1000" CompletionSetCount="20" FirstRowSelected="false" OnClientItemSelected="ClientItemSelected">
</cc1:AutoCompleteExtender>
</div>
<script type="text/javascript">
function StopType(ele) {
if (ele.value.length >= 3) {
ele.setAttribute('disabled', 'disabled');
setTimeout(function () {
ele.removeAttribute('disabled');
ele.focus();
}, 2000);
}
}
function ClientItemSelected(sender, e) {
sender._element.removeAttribute('disabled');
}
</script>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Script.Services;
using System.Web.Services;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.Script.Services
Imports System.Web.Services
Code
C#
[ScriptMethod()]
[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 CustomerID, 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(AjaxControlToolkit.AutoCompleteExtender
.CreateAutoCompleteItem(sdr["ContactName"].ToString(), sdr["CustomerID"].ToString()));
}
}
conn.Close();
return customers;
}
}
}
VB.Net
<ScriptMethod()> _
<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 CustomerID, 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(AjaxControlToolkit.AutoCompleteExtender _
.CreateAutoCompleteItem(sdr("ContactName").ToString(), sdr("CustomerID").ToString()))
End While
End Using
conn.Close()
Return customers
End Using
End Using
End Function
Screenshot