AutoComplete using Handler

<asp:textbox id=”txtAutoComplete” runat=”server”/>

add This to Script

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
       <script src=http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.js; type="text/javascript"></script>
     <script type="text/javascript">
      $(document).ready(function () {
        $("#<%=txtAutoComplete.ClientID %>").autocomplete('select.ashx');
      });

 

Imports System.Web
Imports System.Web.Services

Public Class _select
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        If Not [String].IsNullOrEmpty(context.Request.QueryString("q")) Then
            For Each s As String In GetAutoCompleteValues(context.Request.QueryString("q"))
                context.Response.Write(s + Environment.NewLine)
            Next
        End If

    End Sub
    Private Function GetAutoCompleteValues(ByVal prefixText As String)
        Dim db As New DataClasses1DataContext()
        Dim data = db.tblKitTemplateHeaders.Where(Function(n) n.CustomKitPartNumber.StartsWith(prefixText)).OrderBy _
            (Function(xe) xe.CustomKitPartNumber).Select(Function(c) c.CustomKitPartNumber).ToList()
        Return data
    End Function

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
 End Class
Advertisements

Ajax

First Register ajaxcontrol toolkit
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
 <asp:TextBox ID="txtAutoComplete" runat="server"></asp:TextBox>
        <ajax:AutoCompleteExtender ID="atcExteder" runat="server" TargetControlID="txtAutoComplete"
            ServiceMethod="GetKits" ServicePath="~/AutoComplete.asmx" MinimumPrefixLength="1"
            UseContextKey="true" CompletionInterval="500">
        </ajax:AutoCompleteExtender>

 add this method to autocomplete.aspx.cs

  <System.Web.Services.WebMethod()> _
    Public Function GetKits(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
        Dim db As New DataClasses1DataContext()
        Dim data = db.tblKitTemplateHeaders.Where(Function(n) n.CustomKitPartNumber.StartsWith(prefixText)).OrderBy _
            (Function(xe) xe.CustomKitPartNumber).Select(Function(c) c.CustomKitPartNumber).Take(count).ToList()
        Return data
    End Function