AutoComplete Extension

1. Add Connection String to web.config
<connectionStrings>
    <add name="conn" connectionString="Data Source=SERVER;Initial Catalog=DB; User ID=USER;Password=PASSWORD" providerName="System.Data.SqlClient"/>
</connectionStrings>

2. Insert Control ScriptManager, Textbox and add AutoComplete Extension
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
        </asp:ScriptManager>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" DelimiterCharacters="" Enabled="True" MinimumPrefixLength="2" ServiceMethod="fn_AutoComplete" TargetControlID="TextBox1" EnableCaching="False" ServicePath="">
        </asp:AutoCompleteExtender>
    
    </div>
    </form>
</body>
</html>

3. Code-Behind
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
    Inherits System.Web.UI.Page

   
    System.Web.Services.WebMethod()> _
    Public Shared Function fn_AutoComplete(prefixText As String, count As Integer) As List(Of String)
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("conn").ConnectionString)
        Dim sql As String
        Dim da As SqlDataAdapter
        Dim ds As New DataSet
        Dim result As New List(Of String)

        sql = "select name from emp where name like '" & prefixText & "%' "
        da = New SqlDataAdapter(sql, conn)
        da.Fill(ds, "dt")

        If ds.Tables("dt").Rows.Count > 0 Then
            For i As Integer = 0 To ds.Tables("dt").Rows.Count - 1
                result.Add(ds.Tables("dt").Rows(i).Item("name").ToString)
            Next
        End If
        Return result
    End Function
End Class

4. Demo

Comments

Popular posts from this blog

การตั้งเวลาระหว่าง Server และ Client

วิธีตั้งค่า NTP บน Primary Domain Controller

Installation and Run Node.JS on IIS